/Ge, /Gs (Turn Stack Checking On or Off)

The /Ge option, which applies to all source files that follow it on the command line, enables “stack probes.” A “stack probe” is a short routine called on entry to a function to verify that the program stack has enough room to allocate local variables required by the function. The stack-probe routine is called at every function-entry point. Ordinarily, the stack-probe routine generates a stack-overflow message if the required stack space is not available. When stack checking is turned off, the stack-probe routine is not called, and stack overflow can occur without being diagnosed (that is, no stack-overflow message is printed).

The compiler uses stack probes to guard against possible execution errors. These stack probes are used whenever the /Ge option (the default) is in effect. You can remove the stack probes by using either the /Gs option or the check_stack pragma, which reduces the size of a program and speeds up execution slightly. Note that the /Gs option and the check_stack pragma have no effect on standard C library routines; they affect only the functions you compile.

Use the /Gs option when you want to turn off stack checking for an entire module and you know the program does not exceed the available stack space. For example, stack probes may not be needed for programs that make very few function calls or that have only modest local-variable requirements. In the absence of the /Gs option, stack checking is on. Use the /Gs option with great care. Although it can make programs smaller and faster, it can also make the program unable to detect overflow of the program stack.

Use the check_stack pragma when you want to turn stack checking on or off only for selected routines, leaving the default (as determined by the presence or absence of the /Gs option) for the rest. When you want to turn off stack checking, put the following line before the definition of the function you don't want to check:

#pragma check_stack (off)

Note that the preceding line disables stack checking for all routines that follow it in the source file, not just the routines on the same line. To reinstate stack checking, insert the following line:

#pragma check_stack (on)

If you don't give an argument for the check_stack pragma, stack checking reverts to the behavior specified on the command line: disabled if the /Gs option is given or enabled if it is not. The interaction of the check_stack pragma with the /Gs option is summarized in Table 13.6.

Table 13.6 Using the check_stack Pragma


Syntax
Compiled with /Gs Option?
Action

#pragma check_stack() Yes Turns off stack checking for routines that follow
#pragma check_stack() No Turns on stack checking for routines that follow
#pragma check_stack(on) Yes or no Turns on stack checking for routines that follow
#pragma check_stack(off) Yes or no Turns off stack checking for routines that follow

Note:

For older versions of Microsoft C, the check_stack pragma had a different format: check_stack(+) enabled stack checking and check_stack(–) disabled stack checking. The Microsoft C/C++ compiler no longer accepts this format.

Example

CL /Gs FILE.C

This example optimizes the file FILE.C by removing stack probes with the /Gs option. If you want stack checking for only a few functions in FILE.C, you can use the check_stack pragma before and after the definitions of functions you want to check.