Increase stack size in OS X Lion

后端 未结 2 1110
青春惊慌失措
青春惊慌失措 2021-01-21 09:33

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?

2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-21 10:04

    You can use getrlimit/setrlimit - this works on Linux, Mac OS X, and other POSIX-ish operating systems, e.g.

    #include 
    
    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;
    }
    

提交回复
热议问题