Increase stack size in OS X Lion

荒凉一梦 提交于 2019-12-20 02:32:40

问题


I need to do it for a C++ program that needs a lot of stack. I use g++ (included in OS X Lion) to compile it. How could I increase it for my program?


回答1:


From http://developer.apple.com/library/mac/#qa/qa1419/_index.html

Using gcc, pass link flags through to ld with -Wl:

gcc -Wl,-stack_size -Wl,1000000 foo.c



回答2:


You can use getrlimit/setrlimit - this works on Linux, Mac OS X, and other POSIX-ish operating systems, 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;
}


来源:https://stackoverflow.com/questions/10214363/increase-stack-size-in-os-x-lion

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!