[Return to Bookshelf] [Contents] [Previous Section] [Next Section] [Index] [Help]


3.3.1.2 Stack

Stack memory is allocated by code generated by the language compiler at run time, generally when a routine is initially called. When the program returns from the routine, the storage ceases to be valid (although the addresses still exist and may be accessible).

Generally, the storage is valid for the entire execution of the routine, and the actual address can be calculated and passed to other threads, but this depends on programming language rules. If you pass the address of stack memory to another thread, you must ensure that all other threads are finished processing that data before the routine returns; otherwise the stack will be cleared, and values may be altered by subsequent calls. The other threads will not be able to determine that this has happened and erroneous behavior will result.

The scope of stack memory is the routine or a block within the routine. The lifetime is no longer than the time during which the function executes.