How can one increase the maximum size of a stack for C program that has a deep recursive call? Is there any config for the project where one can specify the stack or heap size for executables?
Asked
Active
Viewed 5,578 times
3
-
1If this is on Linux or other *nix OS then you can set the stack size from within your code - see [this question and answer](http://stackoverflow.com/a/2284691/253056) for code. – Paul R Apr 10 '17 at 15:17
-
I'm doing it on Windows, with Cygwin environment – arslancharyev31 Apr 10 '17 at 15:41
-
1I believe cygwin also supports `getrlimit`/`setrlimit`, so I suggest giving it a try. – Paul R Apr 10 '17 at 15:44
-
I tried copypasting the code in the answer and running recursive function in the end of main, but nothing changed. – arslancharyev31 Apr 10 '17 at 15:57
-
1You may need to adjust `kStackSize` of course - how much stack space do you think that your program needs ? – Paul R Apr 10 '17 at 15:59
-
1Initial thread stack size is set by the linker. You can ask the linker to increase it at build time. – ThingyWotsit Apr 10 '17 at 17:13
4 Answers
7
I solved the problem by adding following linker flag in project's CMakeList.txt
MATH(EXPR stack_size "16 * 1024 * 1024") # 16 Mb
set(CMAKE_EXE_LINKER_FLAGS "-Wl,--stack,${stack_size}")

arslancharyev31
- 1,801
- 3
- 21
- 39
-
1This worked for me too. Note that this can also be done using `target_link_libraries()` or `set_target_properties()` as shown [here](https://stackoverflow.com/a/55989674). – Gumby The Green May 05 '19 at 07:19
-
1@GumbyTheGreen, Nice solution. Have you considered posting it as an answer, so that I can accept it, instead of selfishly accepting my own solution :) – arslancharyev31 May 06 '19 at 14:25
-
Done. It's never too late to change your selfish ways... :) No, I actually found your answer helpful as well and am now using that math expression in my own script. – Gumby The Green May 06 '19 at 19:34
1
To check initial stack size
peflags -x <binary>
to set size
peflags -x<size> <binary>
As reference peflags --help
and
https://cygwin.com/ml/cygwin/2013-08/msg00318.html

matzeri
- 8,062
- 2
- 15
- 16
1
To expand on the OP's own answer, the following three CMake commands all work for me on Windows with MinGW/GCC (replace <target>
with what you entered into add_executable()
):
target_link_libraries(<target> PRIVATE "-Wl,--stack,10000000")
OR
set_target_properties(<target> PROPERTIES LINK_FLAGS -Wl,--stack,10000000)
OR
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--stack,10000000")
According to the CMake documentation, each of these should just add linker flags, not replace any that are already set.
In Visual Studio, you should replace -Wl,--stack,
with /STACK:
in each of these according to this thread and others. For example:
target_link_libraries(<target> PRIVATE "/STACK:10000000")

Gumby The Green
- 603
- 7
- 10
0
on macos (os x) or linux use
-stack_size
like so:
set(CMAKE_EXE_LINKER_FLAGS "-Wl,-stack_size,4000000")
- notice: the number is interpreted as hex

Yuval Zilber
- 131
- 11