nor's blog

By nor, 7 months ago, In English

Disclaimer: Please verify that whichever of the following solutions you choose to use indeed increases the stack limit for the execution of the solution, since different OSes/versions can behave differently.

On Linux (and probably MacOS), running ulimit -s unlimited in every instance of the shell you use to execute your program will work just fine (assuming you don't have a hard limit). On some compilers, passing -Wl,--stack=268435456 to the compiler options should give you a 256MB stack.

But a much easier way from my comment a couple of years ago, originally from here, is to do this in your Meta Hacker Cup template:

#include <bits/stdc++.h>
using namespace std;
void main_() {
    // implement your solution here
}
static void run_with_stack_size(void (*func)(void), size_t stsize) {
    char *stack, *send;
    stack = (char *)malloc(stsize);
    send = stack + stsize - 16;
    send = (char *)((uintptr_t)send / 16 * 16);
    asm volatile(
        "mov %%rsp, (%0)\n"
        "mov %0, %%rsp\n"
        :
        : "r"(send));
    func();
    asm volatile("mov (%0), %%rsp\n" : : "r"(send));
    free(stack);
}
int main() {
    run_with_stack_size(main_, 1024 * 1024 * 1024); // run with a 1 GiB stack
    return 0;
}

It apparently works with Windows as well, as a commenter confirmed, and I believe it should work with MacOS as well.

UPD: To use this on 32 bit systems/compilers, replacing %rsp with %esp works. I haven't tried changing this for ARM, so any solutions which do the same thing for ARM would be appreciated.

  • Vote: I like it
  • +205
  • Vote: I do not like it

»
7 months ago, # |
Rev. 2   Vote: I like it -30 Vote: I do not like it

Just write this on top of code

#pragma comment(linker, "/STACK:1073741824")

UPD: It does not even work on windows MSVC.

  • »
    »
    7 months ago, # ^ |
      Vote: I like it +27 Vote: I do not like it

    Doesn't work with all operating systems, and only with MSVC I believe. Here are some other alternatives.

  • »
    »
    7 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    is it work on widows 10 and sumlime text?

  • »
    »
    7 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Does it work with Ubuntu 22.04?

»
7 months ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

Not sure if it'll be helpful to people, but I have the following function in my ~/.bashrc on Mac (should also work on Linux): https://pastebin.com/VV6DCjhx

Usage is cpprun code.cpp and you can optionally append <file.in and/or >file.out for I/O. This will build and run code.cpp, and show the execution time and exit code. It's convenient to me because it's just one line.

You may have to tweak the line with the g++ build flags. In particular, the flags -Wl,-stack_size -Wl,20000000 is what I've found to work most consistently on Mac for increasing stack limit.

»
7 months ago, # |
  Vote: I like it 0 Vote: I do not like it

I used the template but got the following:

Errors while compiling:
C:\Users\roysh\AppData\Local\Temp\ccBV77oY.s: Assembler messages:
C:\Users\roysh\AppData\Local\Temp\ccBV77oY.s:579: Error: bad register name `%rsp'
C:\Users\roysh\AppData\Local\Temp\ccBV77oY.s:580: Error: bad register name `%rsp'
C:\Users\roysh\AppData\Local\Temp\ccBV77oY.s:589: Error: bad register name `%rsp'

What could be the possible reason for this?

  • »
    »
    7 months ago, # ^ |
    Rev. 2   Vote: I like it +8 Vote: I do not like it

    Do you have a 32 bit machine or 64 bit machine? From the small amount of assembly I remember, I have a feeling this might be an issue. Maybe using esp instead of rsp can help.

    • »
      »
      »
      7 months ago, # ^ |
      Rev. 3   Vote: I like it 0 Vote: I do not like it

      I have a 64 bit machine but maybe 32bit MinGW is installed, never bothered before...

      Could you please share or let me know any tutorial for 64bit cpp installation? Would be really helpful...

      UPD: Worked after using 'esp' in place of 'rsp'... , Thanks

»
7 months ago, # |
  Vote: I like it +5 Vote: I do not like it

Why and How does this works?

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

    It allocates a stack and manually changes the stack pointer before and after the function call.

»
7 months ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it

ok now I know why my solution crashed in my computer during hacker cup 2021 but later it worked on other people's computers , thanks

»
7 months ago, # |
  Vote: I like it 0 Vote: I do not like it

On Linux, using ulimit -s unlimited may not be the wisest choice because of infinite recursion. It is preferable to specify a size.

»
7 months ago, # |
  Vote: I like it +6 Vote: I do not like it

I just use this while compiling c++ file in windows: g++ "-Wl,--stack,1078749825" file_name.cpp -o output

  • »
    »
    7 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Does it work with the , instead of =? Seems a bit different from what I know.

  • »
    »
    7 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    This works with Windows 11.

»
7 months ago, # |
Rev. 2   Vote: I like it -13 Vote: I do not like it

In Facebook Hacker Cup 2022 Round 2, I coded a right O(n) solution (C++) for Problem A, but I downloaded the input test case and ran the code. but it took me around 8 minutes to get the output. As a result, I couldn't submit the output. However, after the contest, I submitted the output and got accepted. Can anyone please tell me how can I compile fast or overcome this problem or any suggestions regarding this?

»
7 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Thanks a ton!

»
7 months ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

not working in codeblocks

I really need it, a program won't run because I have an array that's way too big. Any idea what I could do, and do it fast?

  • »
    »
    7 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Try declaring array globally. It might work

  • »
    »
    7 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    What is the error message? It should not be an issue with an IDE, but with the OS/architecture of your machine/target of compiler. If it is Windows and your compiler is 32 bit instead of 64 bit, you can try using esp instead of rsp. If you're using a machine with an ARM chip, this won't work.

    • »
      »
      »
      7 months ago, # ^ |
      Rev. 3   Vote: I like it 0 Vote: I do not like it

      it wasn't an error message. Here is more context:

      I had a class containing a vector of a struct with 2 ints and a double of size 1e6. Although it did compile, it immediately crashed when I tried to run it. When I tried to catch where it crashed with F8 (a shortcut in CodeBlocks) it didn't catch anything and just failed, leaving me to wonder wtf is causing it. When I took everything out of the class, it all ran fine.

      I don't know what esp, rsp or an ARM chip are. I'll try to read up on it lol

      Here is what I got when I tried to run the code you provided here, sorry but I have no idea what to make of these errors lmao https://ibb.co/CMw2twK

      • »
        »
        »
        »
        7 months ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        The struct should not have been an issue. The screenshot looks like a compilation error, seems like the error is in the debug build instead.

        One thing you can try is find and replace "rsp" in the code above by "esp" — if your execution crashed due to architecture issues (32 bit vs 64 bit), then it should most likely be fixed by this change.