This post was originally written on Codeforces; relevant discussion can be found here.
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.