INF: The Cause of Run-Time Error R6000 Stack Overflow

ID Number: Q32816

4.00 5.00 5.10 6.00 6.00a 6.00ax 7.00 | 5.10 6.00 6.00a

MS-DOS | OS/2

Summary:

In Microsoft C versions 4.0, 5.0, 6.0, 6.0a, 6.0ax, and C/C++ version

7.0, the run-time error R6000 Stack Overflow can be caused by two

different problems, as follows:

- Your program's stack is not large enough to hold all the data

being pushed on it during execution of your program, so the stack

overflows. This problem can be caused by heavily recursive

programs and programs that declare large amounts of data on

the stack ("local" or "automatic" data in C jargon).

In this case, you need a larger stack or less recursion and/or less

local data.

- The second cause for the R6000 error is somewhat counter

intuitive; it can be caused by the C startup (initialization)

code when it tries to allocate space for the stack and can't do so.

In this case, you need to reduce the size of your stack or reduce

the amount of data in DGROUP.

More Information:

The following is a description of both problems, ways to solve the

problem, and a method for determining which problem you are

encountering:

Case 1: R6000 Occurs at Run Time

--------------------------------

The stack overflows because too much information is pushed on it.

This information may be either function-return addresses or local

data. Each time a function is called, the return address in the

calling function is pushed on the stack along with any parameters;

then, when the called function executes, it may allocate local

("automatic") data for its own use. This process requires stack space.

To correct this problem, do one of the following:

- Decrease the number of local variables, perhaps by declaring

those variables as static so they will not be pushed on the

stack.

- Increase the stack size by compiling with the /F x option, where x

is a hexadecimal number representing the number of bytes desired in

the stack size (see page 102 of the "Microsoft C Optimizing

Compiler User's Guide" for version 5.1).

- Change the stack size by linking with the /STACK switch, or using

the EXEMOD utility. Note that increasing the stack size too much

can cause an R6000 as described in case 2.

Case 2: R6000 Occurs at Startup

-------------------------------

The startup code allocates space for the stack in the segment DGROUP.

If DGROUP does not contain room for the specified stack size (default

= 2K), the startup code issues the R6000 error.

To correct this problem, either reduce the size of the stack or reduce

the amount of data in DGROUP. To reduce the stack size, compile with

the /F option, or link with the /STACK option, or use the EXEMOD

utility. To reduce the amount of data in DGROUP, try switching from a

small-data model (small- or medium-memory model) to a large-data model

(compact-, large-, or huge-memory model). If you already are in a

large-data model, compile with the /Gt switch to move data from DGROUP

to far data segments.

To use the /Gt switch, specify /Gtx, where x is some decimal value

representing a number of bytes. Data items larger than x bytes are

allocated a new segment, thereby freeing up more space in DGROUP for

the stack. For more information, see page 156 of the "Microsoft C

Optimizing Compiler User's Guide" for version 5.1.

How to Determine What Is Causing the R6000 Error

------------------------------------------------

An excellent way to determine the cause of the problem is to use the

CodeView debugger. After invoking CodeView on your program, execute to

the beginning of function main() by doing one of the following:

- Single-step with F8 or T.

- Enter "g main" at the CodeView prompt.

When you've executed past the open curly-brace of main(), the C

startup code has done its job by allocating space for stack and data.

If the R6000 error does not occur at this time, you are experiencing

case 1, a run-time stack overflow (as opposed to case 2, a

startup-time stack overflow); you can now take appropriate action as

described above.

Additional reference words: 5.00 5.10 6.00 6.00a 6.00ax 7.00