Stack and Heap are the two sections of the memory layout of a process.
The stack is used to keep track of variables/parameters local to a function in a program.
Whenever you call a new function, a new stack frame is pushed to the stack with parameters and variables local to that function. When that function returns, the stack frame is popped out and the context switches back to the previous function
A heap is a kind of a global memory pool. A function can allocate memory on the heap if it wants the data to live longer than the function itself. Objects allocated on the heap are accessible to all the functions, given they have the reference/address of the object to access it.
No comments:
Post a Comment