Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to overcome Stack Size issue with Visual Studio (running C codes with big array)

I am using Visual Studio 13 to compile c codes for the first time. The codes run perfectly O.K. with 2d arays of size 64*64 (there are a few arrays in my programme) but if I increase the array size to 128*128 it does not run (but compile correctly). Instead it gives a message ".exe has stopped working". My machine has 4GB ram and the same programme run with 128*128 array if I run the codes from linux.

Let me provide some more details: I have run the same code from linux using Intel C Compiler (non-commercial version) in the same machine. But due to some problem I am now constrained to work from a Windows environment. I searched and have installed two c- compilers (1) Visual Studio 13 and (2) Borland C. Both work well with a small array. But the moment I increase array size Visual Studio give the message ".exe has stopped working". I compile the programme using 'cl' from "Developers Command Prompt VS 13".

I feel the problem is with stack size.

In the link detailed explanation (as provided below) I have seen a command "ulimit" used in linux environment to increase the stack size. I remember using it a few years ago.

I feel we are close to the solution, but my problem with Windows (and VS 2013) persists as I failed to execute dumpbin /headers executable_file or editbin /STACK:size. Actually I feel I do not know how to execute them. I tried to execute them from "Developer Command Prompt VS 13" as well as using Run (windows start bottom->search (run)->Run (prop up)). I request you kindly to provide more details if possible.

I searched and found this website and think here the solution can be found.

Please help. I want to run using Visual Studio 13 from Windows.

like image 712
user3129690 Avatar asked Dec 23 '13 13:12

user3129690


People also ask

What is max size of stack in C?

The default stack size is 256 bytes. You can change the stack size at link time by using the --stack_size option with the linker command.

Can I increase stack size?

You may need to increase the stack size if your program gets stack-overflow messages at runtime. You can also set the stack size by: Using the /STACK linker option. For more information, see /STACK (Stack allocations).

How do you calculate maximum stack size?

You can query the maximum process and stack sizes using getrlimit . Stack frames don't have a fixed size; it depends on how much local data (i.e., local variables) each frame needs. To do this on the command-line, you can use ulimit.


1 Answers

It seems that the reason behind this is the stack overflow. The issue can be resolved by increasing the stack size.
In visual studio you can do this by using /STACK:reserve[,commit]. Read the MSDN article.


For more detailed explanation:

Under Windows platforms, the stack size information is contained in the executable files. It can be set during compilation in Visual studio C++.
Alternatively, Microsoft provides a program editbin.exe which can change the executable files directly. Here are more details:

Windows (during compilation):

  1. Select Project->Setting.
  2. Select Link page.
  3. Select Category to Output.
  4. Type your preferred stack size in Reserve: field under Stack allocations. eg, 32768 in decimal or 0x20000 in hexadecimal.

Windows (to modify the executable file):

There are two programs included in Microsoft Visual Studio, dumpbin.exe and editbin.exe. Run dumpbin /headers executable_file, and you can see the size of stack reserve information in optional header values. Run editbin /STACK:size to change the default stack size.

like image 58
haccks Avatar answered Nov 15 '22 16:11

haccks