All variables inside functions (main is also a function) are stored on stack withing function frame. When u enter a function new frame is made on stack in the following order
- first, function arguments are pushed to stack
- then return address is pushed to stack
(push pc)
- base pointer (computer register which is used to point to function frame) is pushed to stack, so the old base (frame) pointer of the outer frame is saved on stack.
(push bp)
- base pointer is set to point to location where stack pointer is pointing
(mov bp, sp)
- then space for local variables is reserved and computer access them via base pointer relatively
So, when you write
int var_int[5];
int* var_intptr = new int[10];
5x4 bytes (or more, whatever the machine is) are reserved for var_int.
4 bytes (varying on the machine) are reserved for var_intptr pointer to int.
So the pointer (variable containing the target's address) is placed on stack, but the sole object(s) made with operator new are located on heap.
When you exit the function, the local variables and arguments are popped out of the stack, and the reference to instantiated object might be lost, but the object isnt deleted.