(a)
void MyMethod()
{
int myInt;
myInt = 5;
} // (myInt doesn't need to be removed from memory manually)

(b)
void MyMethod()
{
    int * pmyInt = new int;
    *pmyInt = 5;
    delete pmyInt;  // must delete the pointer to prevent a memory leak!
}

Example 1: (a) Using a variable on the stack; (b) using a variable on a heap.

Back to Article