During program execution, automatic variables and other data are stored on the program stack. Therefore, the stack grows and shrinks dynamically during execution.
The linker reserves space for the program stack. The default amount of the space reserved by the linker depends on your operating system.
You can override the default stack allocation by specifying option link(stack, reserve) when you build. Inlining functions can affect the size of the program stack.
The compiler maintains efficient stack alignment for improved performance. You may have to specify #pragma stack_align in your source if your code may be called from source that was not compiled with VisualAge C++.
Stack probes add some overhead to your program, but ensure that the stack always grows correctly.
For the purpose of this discussion, the stack grows down from high memory.
How the Stack
Grows
The number of pages committed, or phyically allocated, for the
stack depends on your operating system:
When the guard page is accessed, the following out-of-stack exception occurs:
XCPT_GUARD_PAGE_VIOLATION
STATUS_GUARD_PAGE_VIOLATION
To handle the exception, the system tries to get another guard page below the one previously allocated. If the attempt is successful, the original guard page becomes a normal stack page and the next uncommitted page becomes the new guard page, as shown below:
| attempt to access guard page | after stack growth | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
This process continues until a new guard page can no longer be allocated.
If the system cannot set a new guard page because it has reached the size limit of the stack, the following guard page allocation failure exception occurs:
XCPT_GUARD_PAGE_VIOLATION
XCPT_UNABLE_TO_GROW_STACK
The same exception is generated when the _alloca function runs out of memory.
On
OS/2, the final guard page is reserved for handling exceptions.
If a guard page exception occurs and not enough stack remains to
handle the exception, the program terminates.
The Role of
Stack Probes
Stack probes provide a mechanism for the stack to grow by more
than one page at a time. For the stack growth mechanism to work
correctly, each 4K page must be accessed in the correct order. To
ensure the correct access, the compiler generates one or more
stack probes in the prolog of each procedure that has automatic
storage greater than 2K. Stack probes start after 2K because
exception handling may require up to an additional 2K of stack
storage.
Without stack probes, accessing the stack below the guard page is an access violation and results in program termination.
Because stack probes are added to the prolog of every function that uses more than 2K of stack, your program runs faster when you turn probes off. You can turn off stack probes with gen(probe, no). However, it is not safe to turn off stack probes unless you meet one or more of the following criteria:
VisualAge C++ supports automatic stack growth by default. The compiler also ensures correct stack growth in the following ways:
![]()
Inlining
Signals and
Exceptions
Sections and Segments
Multithreaded Programs
![]()
Set Build Options
Build
Optimize
Your Program
