C++ Memory Management Reminders

New::Delete as Malloc::Free

 

Creating Objects

w/ Default Constructor:

 

One Object

Array of Objects

On Stack:

Obj a;

Obj a[10];

On Heap:

Obj* a = new Obj;

Obj* a = new Obj[10];

w/ Named Constructor:

 

One Object

Array of Objects

On Stack:

Obj a(x);

Obj a[] = {Obj(x), Obj(y)};

On Heap:

Obj* a = new Obj(x);

Obj* a = new Obj[10];

a[0].initFxn(x);

a[1].initFxn(y);

 

Calling Methods:

 

One Object

Array of Objects

On Stack:

a.doSomething();

a[0].doSomething();

On Heap:

a->doSomething();

a[0].doSomething();

 

Deleting objects

 

One Object

Array of Objects

On Stack:

(automatic)

(automatic)

On Heap:

delete a;

delete [] a;