Updated: 2003-01-19 05:50

Changing the Run-Time Stack and Heap Size

Last revised: Sunday January 19, 2003 05:50.

If you've written your program in a memory-intensive way, you may find yourself facing mysterious processor exceptions when you run out of C language run-time stack space or heap space. This can happen if you have recursive functions, and it can happen much faster if the recursive functions have large amounts of run-time stack storage or heap storage, e.g.

myfunc(){
   char buf[1000];
   ...
      myfunc();
}

Each call of myfunc() eats up over 1,000 bytes of run-time stack space. This next implementation isn't much better:

myfunc(){
   char *buf = malloc(1000);
   ...
      myfunc();
   free(buf);
}

This time, it's the heap space that is in danger of running out. In either example, if the recursion nests more than 64 levels, you're guaranteed to exceed some 64k program segment on an IBM PC architecture. (Unix users don't have this 64k limitation.)

Step One is to try to get rid of the large buffers in recursive functions by rethinking your algorithm.

Step Two, if you can't rethink your algorithm, is to replace the stack storage with malloc/free, though if you still try to malloc more than 64k of storage you may hit the same PC architecture limit.

If after all that your program still doesn't work, try the following:

Detecting Stack Overflow in Borland C++ v4.5x

From: Shaun Covell <cove0002@algonquincollege.com>
Subject: BC45 (Target Easywin)

I found out that under BC45 there is a stack restriction that does not exist in
Linux (in 95 I was overwriting my data segment).  One way to test and
fix this problem that I found was as follows: 
        1. Under the Options:Project:Compiler:Debugging menu
                check the box labeled "Test stack overflow"
        2. If after doing number one they recieve the above message
           Add a line in the .def file (which I had to create) that
           says "STACKSIZE n" where n is large enough for many recursive
           calls.

Changing the Run-Time Stack and Heap Size in Borland C++ v4.5x

Submitted by: Paul G. Cumming di316@freenet.carleton.ca

Create a definition file (.DEF) for your project. Create a text file with the same name as the project file
(.IDE), but with the extension .DEF. Put these lines inside it:

STACKSIZE      (stacksize)
HEAPSIZE       (heapsize)

where (stacksize) and (heapsize) are the sizes, in bytes, of the stack and heap. Their size, when added together (for EasyWin projects) cannot exceed or equal 65,536 bytes. Example:

HEAPSIZE 16384
STACKSIZE 16384

Changing the Run-Time Stack and Heap Size under Unix

Unix doesn't have a per-program limit on the run-time stack or heap space; however, the system does place limits on the amount of virtual memory a process may consume.

If you run a Unix variant at home, you may (rarely) need to adjust the stack or data segment limits. Usually this can be done once for your whole login session using a shell command, such as the C shell's limit and unlimit commands:

% limit
cputime         unlimited
filesize        unlimited
datasize        21984 kbytes
stacksize       512 kbytes
coredumpsize    unlimited
memoryuse       unlimited
% unlimit
% limit
cputime         unlimited
filesize        unlimited
datasize        21984 kbytes
stacksize       21984 kbytes
coredumpsize    unlimited
memoryuse       unlimited

After you have done this, re-run your program. (You do not need to recompile your program.) You will need to unlimit the resources again next time you log in. Place the commands in your .login or .profile and you'll have them every time you log in.

Note that if your program dumps core with a large data and/or stack segment, the resulting core file can be very large. Be careful.

Last revised: Sunday January 19, 2003 05:50.
Email comments to Ian! D. Allen
idallen@freenet.carleton.ca

Web Author: Ian! D. Allen idallen@idallen.ca      Updated: 2003-01-19 05:50

Internet Free Zone Level 1 logo Support free and non-commercial Internet.

Any Browser logo This site works best in Any Browser, a campaign for non-specific WWW.

Creative Commons License logo This work is licensed under a Creative Commons License.