The stack is used when you do not use
or other related memory allocation functions explicitly.
int x;//<--- stack space is used.
int *x = new int();//<--- heap space is used.
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;//<--- allocated on the stack
Anything allocated on the heap must be freed. For C allocations use
, for C++ allocations use
You can also find my answer here which gives a lot more differences between the stack and the heap