Slow_But_Determined's blog

By Slow_But_Determined, history, 6 years ago, In English

I usually write:

'#define N 1000000

vector adj[N];

But when I run it locally, I get segmentation fault due to stack size being too small, so I end up reducing N to something like 10000 and forget to change it back again before submitting, causing unnecessary penalties.

So is there anyway to permanently increase stack size in ubuntu?

g++ -Wl,--stack=268435456 file.cpp works in Windows.

ulimit -s unlimited only increases stack size for the specific terminal

UPD:

Thanks a lot for your replies, I ended up spilling water on my laptop :(

I'll try the suggestions when (if?) My laptop gets dried.

»
6 years ago, # |
  Vote: I like it +15 Vote: I do not like it

Add ulimit -s unlimited to .bashrc?

  • »
    »
    6 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Thanks, but that works only if I run through a terminal. Is there any way to make it persistent even when running through sublime text?

    • »
      »
      »
      6 years ago, # ^ |
      Rev. 2   Vote: I like it +5 Vote: I do not like it

      Here is the build file I am using for Sublime Text Build 3176 on Ubuntu 18.04.1 LTS.

      You just have to add ulimit -s unlimited; to Run command.

    • »
      »
      »
      6 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      you can try .profile. As far as I remember it is sourced during graphical login too.

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

you can do that from code (with smth like this)

»
6 years ago, # |
  Vote: I like it +3 Vote: I do not like it

Why not modify the /etc/security/limits.conf file? A line like

your-username     soft    stack    1048576

should set your (soft) stack limit for all your processes to 1 GB by default. (The change might require a log-out/log-in in order to take effect.) For more info, please refer to man limits.conf.

  • »
    »
    6 years ago, # ^ |
      Vote: I like it +5 Vote: I do not like it

    Why not modify the /etc/security/limits.conf file?

    Why modify such things globally?

    As for me, I am using Geany, and wrote something like

    bash -c 'ulimit -s 262144 && ulimit -v 1048576 && time "./%e"'
    

    for running programs.

    The first part sets the stack size, the seconds one sets the memory limit (to prevent memory from exhausting because of some bugs), the last one allows to see the total time.

    • »
      »
      »
      6 years ago, # ^ |
        Vote: I like it +10 Vote: I do not like it

      This worked perfectly!

    • »
      »
      »
      4 years ago, # ^ |
        Vote: I like it -39 Vote: I do not like it

      brother, what can I do to run 10^10 operation? I am using Geany also.

    • »
      »
      »
      4 years ago, # ^ |
        Vote: I like it -36 Vote: I do not like it

      How can i add this  --stack,268435456 in vim compilation flag in linux !?

      • »
        »
        »
        »
        4 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        You cannot, since this linker flag is Windows only. AFAIK the stack size on Linux is known only when the program starts to execute.

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by Slow_But_Determined (previous revision, new revision, compare).