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.
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.Yes, I did it, But It didn't work somehow
I added this->
@my_username soft stack 2000000
inlimits.conf
, Is there any mistake in this line?I think you should not put @ before your username.
I added this
my_username soft stack 262144
and it worked well.Thanks bro, My bad, It worked
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
Yeah, sad!
Given link work only for codeblocks. Is there any way/command-flag which i can use in VScode for this purpose?
I don't use codeblocks, just edited my compilation flags, whatever IDE u might be using, search in your setting how to edit your compilation flags and it should work fine
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()
andsetrlimit()
system calls: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.Thank you very much!
Is it a good idea to have stack size something as big as 1 gb when default size is like 8MB?
Does it have any side-effects?
If you have infinite recursion, then your program crashes much later, and possibly after consuming all system memory.
Suggestion: Leave it as default and raise only when needed in individual cases.
Does the same occur on increasing it not to rlim_max but to 1 Gb instead ?
P.S — just curious because i added it in my snippet and made limit to 1 Gb and it should return segmentation fault after crossing the 1 Gb limit but I'm not in mood to try after reading your Comment.
If you increase it to 1 GiB, then the program will segfault if it tries to exceed this limit. If you always have 1 GiB of free memory while solving contests, everything should be fine.
Also I noticed that I made a tiny mistake in the comment above, which is fixed now.
setrlimit()
takes stack size in bytes, so 1 GiB would berlim.rlim_cur = 1024 * 1024 * 1024
. If you added this code into your snippet, it's better to fix it.I use the following wrapper to run the solutions locally:
ulimit -v
is used to limit the amount of memory, so I won't go out of memory in case of infinite memory allocation.I failed D2 because of the exact same issue :(
Damn, now I feel like this blog is important for many coders :P
Maybe you can just add
ulimit -s new_value
to your.bashrc
file?