| March 9, 2009 1:00 AM PDT | |
Automatically allocate stack space in Windows 2000 (64-bit) applications to avoid stack overflow. The operating system permits multiple threads of execution within a process's address space. Each thread is given its own execution context, including its own call/return stack. These stacks must be of sufficient size to store all of the likely call chains the thread might experience while it is running.
The operating system will also generate a guard region at the top of the allocated stack space that will cause a runtime error if it is accessed; this is a means to inform you if the thread's stack has been exceeded and thus to avoid unobserved damage to data owned by other threads or data structures in the process.
Because 64-bit applications will, in general, pass larger-sized items in function parameters than their 32-bit counterparts, migration to Itanium® architecture requires the assessment of stack- and guard- region sizes. The following code sample would cause a stack overflow when compiled for 64-bit Intel architecture:
int g_cnt=0; |
Line 3 of the code sample above turns the stack probes off. The code then defines and initializes the array that allocates the stack space. The counter is incremented, and the array is recursively called the number of times specified in MAX_ITERATIONS. At line 13, the stack probes are normally turned on automatically; the command to turn on stack probes was added here for clarity.
In the last few lines of code, 1024 bytes of stack are allocated. ThreadFunc uses 4096 bytes every time it is called. Thus, by the 250th iteration, the allocated stack space of 1MB is used up, and the 251st iteration causes a stack overflow.
Use /Ge for global checking or #pragma check_stack(on) for checking specific functions. If you decide to increase performance by disabling stack probing, look for thread creation and manually check the stack sizes to ensure that they are large enough.
The following Windows 2000 (64-bit) code sample shows how you might allocate enough thread-stack space while monitoring functions using stack probing:
int g_cnt=0;<br /> |
In this code, tack probing automatically allocates stack space for each iteration after the 250th. (Normally, stack probes are automatically turned on; #pragma check_stack(on) in line 3 is inserted for clarity.)
Preparing Code for the IA-64 Architecture (Code Clean)
For more complete information about compiler optimizations, see our Optimization Notice.

