Increasing stack size for a C program

In java, when there is a problem, usually with heap, you simply throw in -Xmx and for stack, it would be -Xss. But what about a C program? Recently I ran into this problem. I had a C program which worked fine on one enterprise class Linux machine but didn’t work on my personal computer based on Mac OS X (Snow Leopard). First I didn’t understand what was going since the error wasn’t obvious but soon I realized that the problem was with the stack as it happened for larger values of input. When you do

ulimit -s

you would notice something like


Then setting

ulimit -s hard

and then checking

ulimit -s

you would notice something like


So, with this increased stack size, I was able to run the program for even higher input values but soon ran into the same issue as the input value increased even further. Then, searching the web indicated that it’s possible to actually provide a hint at the time of compilation of how much stack size the program would need. So, all I had to do was to compile the program as follows

g++ -O3 -Wl,-stack_size,0xf000000 -o my_program

The 0xf000000 above is the stack size. It’s probably way more than I need. This solved my problem. While this is a good workaround, I think the Java solution is better as it doesn’t require recompiling the code. But then, java is an interpreter, so it has that extra flexibility.

Leave a comment

Filed under C/C++, Java

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s