Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 2501 Review Declaring a variable allocates space for the type of datum it is to store int x; // allocates space for an int int *px; // allocates space.

Similar presentations


Presentation on theme: "CSE 2501 Review Declaring a variable allocates space for the type of datum it is to store int x; // allocates space for an int int *px; // allocates space."— Presentation transcript:

1 CSE 2501 Review Declaring a variable allocates space for the type of datum it is to store int x; // allocates space for an int int *px; // allocates space for a pointer to an int Pointer and pointee are different –Space for pointee must be allocated before pointer dereferencing is sensible Assignment to a pointer copies address, not contents

2 CSE 2502 Where am I? The & operator yields the address of a variable Examples: int x = 12; int * px; px = &x; // assigns to px the address of x

3 CSE 2503 “Clean up after yourself!” C++ does not have automatic garbage collection like Java does It is the programmer’s responsibility to return to free space any memory reserved using the new operator. Memory allocated using new must be freed using delete.

4 CSE 2504 delete operator int * px; px = new int(3); … do some work until finished with the space px points to… delete px;

5 CSE 2505 Memory areas memory allocated to other process static allocation stack (grows down) (unused memory) heap (grows up) memory allocated to other process

6 CSE 2506 Classes Syntax to define classes in C++ is similar but not identical to that in Java. The class is declared independently of its implementation.

7 CSE 2507 Class declaration class Point { public: Point(int,int); int getX(); int getY(); void setX(int); void setY(int); private: int x, y; };


Download ppt "CSE 2501 Review Declaring a variable allocates space for the type of datum it is to store int x; // allocates space for an int int *px; // allocates space."

Similar presentations


Ads by Google