Блог пользователя shashankagrawal

Автор shashankagrawal, история, 6 лет назад, По-английски

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?

I tried changing this, but It didn't work. I faced this issue in hackercup qualification round D2, terminal gave segfault, and later I came to know about the low value of default stack size in linux.

  • Проголосовать: нравится
  • +81
  • Проголосовать: не нравится

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +21 Проголосовать: не нравится

After altering limits.conf, have you tried logging off and logging back into your account? I believe the changes won't come into effect unless you do this.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +18 Проголосовать: не нравится

I feel u bro, same thing happened to me, but even worse I was on Windows using VS code, spent half an hour trying to find what's the problem :( then after a long search edited my compilation flags as shown here

»
6 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится +25 Проголосовать: не нравится

Just typing ulimit -s into terminal should give you the default stack size. For me, it's 8192, i.e. 8 MiB.

To change the stack size in the code, you can use getrlimit() and setrlimit() system calls:

#include <sys/resource.h>

int main() {
    rlimit rlim;
    if (getrlimit(RLIMIT_STACK, &rlim)) return 1;
    rlim.rlim_cur = rlim.rlim_max;
    // You can set the fixed value instead of max value, e.g. rlim.rlim_cur = 1024 * 1024 * 1024
    // will set your stack size to 1 GiB
    if (setrlimit(RLIMIT_STACK, &rlim)) return 2;

    // Your code here...

    return 0;
}

Note that this example will work only for UNIX-like systems, so it's better to disable this code (using #ifdef, for example) when submitting to an online judge.

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +29 Проголосовать: не нравится

I failed D2 because of the exact same issue :(

»
5 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Maybe you can just add ulimit -s new_value to your .bashrc file?