70 likes | 331 Views
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
E N D
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 CSE 250
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 CSE 250
“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. CSE 250
delete operator int * px; px = new int(3); … do some work until finished with the space px points to… delete px; CSE 250
Memory areas CSE 250
Classes • Syntax to define classes in C++ is similar but not identical to that in Java. • The class is declared independently of its implementation. CSE 250
Class declaration class Point { public: Point(int,int); int getX(); int getY(); void setX(int); void setY(int); private: int x, y; }; CSE 250