The stack is used when you do not use
new
or
malloc
or other related memory allocation functions explicitly.
For example:
int x;
int *x = new int();
Relating to function calls, the function call, and the parameters passed in, and the variables declared inside the function are all on the stack unless you explicitly allocate them on the heap.
void myFunc(int x)
{
int y;
}
Anything allocated on the heap must be freed. For C allocations use
free
, for C++ allocations use
delete
or
delete[]
for arrays.
You can also find my answer here which gives a lot
more differences between the stack and the heap.