Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change stack size for a C++ application in Linux during compilation with GNU compiler

Tags:

gcc

gnu

In OSX during C++ program compilation with g++ I use

LD_FLAGS= -Wl,-stack_size,0x100000000

but in SUSE Linux I constantly get errors like:

x86_64-suse-linux/bin/ld: unrecognized option '--stack'

and similar.

I know that it is possible to use

ulimit -s unlimited

but this is not nice as not always can a single user do that.

How can I increase the stack size in Linux with GCC for a single application?

like image 299
asdf Avatar asked Feb 16 '10 19:02

asdf


People also ask

How do I change the stack size in linux?

We can change stack size in linux temporarily by using the command ulimit -s new_value , But is there any way to change stack size permanently, or Can we do something within C++ code, so it will change stack size by itself?

Can we change the stack size?

You can do this by using the "ulimit -s" command in linux which will set the stack size for all process executed under that shell. Incase of windows, the same can be done in VC6 for that project by setting the stack size in Project Properties->link options->output->stack allocations->reserve.

How do you specify stack size?

You may need to increase the stack size if your program gets stack-overflow messages at runtime. You can also set the stack size by: Using the /STACK linker option. For more information, see /STACK (Stack allocations).


5 Answers

You can set the stack size programmatically with setrlimit, e.g.

#include <sys/resource.h>

int main (int argc, char **argv)
{
    const rlim_t kStackSize = 16 * 1024 * 1024;   // min stack size = 16 MB
    struct rlimit rl;
    int result;

    result = getrlimit(RLIMIT_STACK, &rl);
    if (result == 0)
    {
        if (rl.rlim_cur < kStackSize)
        {
            rl.rlim_cur = kStackSize;
            result = setrlimit(RLIMIT_STACK, &rl);
            if (result != 0)
            {
                fprintf(stderr, "setrlimit returned result = %d\n", result);
            }
        }
    }

    // ...

    return 0;
}

Note: even when using this method to increase stack size you should not declare large local variables in main() itself, since you may well get a stack overflow as soon as you enter main(), before the getrlimit/setrlimit code has had a chance to change the stack size. Any large local variables should therefore be defined only in functions which are subsequently called from main(), after the stack size has successfully been increased.

like image 198
Paul R Avatar answered Oct 02 '22 16:10

Paul R


Instead of stack_size, use --stack like so:

gcc -Wl,--stack,4194304 -o program program.c

This example should give you 4 MB of stack space. Works on MinGW's GCC, but as the manpage says, "This option is specific to the i386 PE targeted port of the linker" (i.e. only works for outputting Windows binaries). Seems like there isn't an option for ELF binaries.

like image 43
AndiDog Avatar answered Oct 02 '22 16:10

AndiDog


This is an old topic, but none of the flags answered here worked for me. Anyway by I found out that -Wl,-z,stack-size=4194304 (example for 4MB) seems to work.

like image 36
Rof Avatar answered Oct 02 '22 16:10

Rof


Consider using -fsplit-stack option https://gcc.gnu.org/wiki/SplitStacks

like image 26
Sergei Krivonos Avatar answered Oct 02 '22 15:10

Sergei Krivonos


Change it with the ulimit bash builtin, or setrlimit(), or at login with PAM (pam_limits.so).

It's a settable user resource limit; see RLIMIT_STACK in setrlimit(2).

http://bytes.com/topic/c/answers/221976-enlarge-stack-size-gcc

like image 32
f0ster Avatar answered Oct 02 '22 14:10

f0ster