![]() |
![]() |
![]() |
![]() |
![]() |
|
The following sequence shows how the stack grows and shrinks. The first diagram in the sequence shows the stack just before a function is called:
*---------*---------*---------*---------*---------*---------* | In use | In use | In use | Unused | Unused | Unused | *---------*---------*---------*---------*---------*---------*
Next, the stack grows to accommodate parameters and local variables as a function is called; in the diagram, two chunks are pushed on to the end of the stack, overwriting unused memory:
Reserved for a
new function call
|
v
-------------------
*---------*---------*---------*---------*---------*---------*
| In use | In use | In use | In use | In use | Unused |
*---------*---------*---------*---------*---------*---------*
Next, the stack shrinks when the function returns; memory previously used for parameters and local values becomes unused and ready for reuse:
Ready for reuse
|
v
-------------------
*---------*---------*---------*---------*---------*---------*
| In use | In use | In use | Unused | Unused | Unused |
*---------*---------*---------*---------*---------*---------*
Finally, the stack grows again to accommodate the next function call, and memory previously occupied is overwritten; the new function call happens to require three chunks of memory:
Reserved for a
new function call
|
Overwritten |
| |
v |
------------------- v
-----------------------------
*---------*---------*---------*---------*---------*---------*
| In use | In use | In use | In use | In use | In use |
*---------*---------*---------*---------*---------*---------*