7
CSE 250 1 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

Review

  • Upload
    luke

  • View
    59

  • Download
    0

Embed Size (px)

DESCRIPTION

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 - PowerPoint PPT Presentation

Citation preview

Page 1: Review

CSE 250 1

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

Page 2: Review

CSE 250 2

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

Page 3: Review

CSE 250 3

“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.

Page 4: Review

CSE 250 4

delete operator

int * px;

px = new int(3);

… do some work until finished with the space px points to…

delete px;

Page 5: Review

CSE 250 5

Memory areas

memory allocated to other process

static allocation

stack (grows down)

(unused memory)

heap (grows up)

memory allocated to other process

Page 6: Review

CSE 250 6

Classes

• Syntax to define classes in C++ is similar but not identical to that in Java.

• The class is declared independently of its implementation.

Page 7: Review

CSE 250 7

Class declaration

class Point{

public:Point(int,int);int getX();int getY();void setX(int);void setY(int);

private:int x, y;

};