No Large Local Variables in Functions

I had been driven crazy by the following code snippet:

void AFunction() {
  char buffer[kBufferSize];

I built this program using GCC successfully under Cygwin and SUSE Linux. However, on either platform, at the running time, whenever AFunction is invoked, the program crashes without prompt message. Even the core dump does not contain any useful information. This Disaster finished until I changed buffer into a static variable:

void AFunction() {
  static char buffer[kBufferSize];

This core dump is due to overflow of the stack (buffer cost more memory than the stack size). This text explains how to change the default stack size on different platforms:

In general, under Unix-like platforms, the stack size is controlled
by environment variable, not the program itself.
So you cannot pass any flags to the
compilers, like gcc, to setup stack size. Under Windows platforms, the
stack size information is contained in the executable files. It can be set
during compilation in Visual C++, but this is not available in gcc.
Alternatively, Microsoft provides a program “editbin.exe” which can change the
executable files directly. Here are more details:

> limit # shows the current stack size
> unlimit # changes the stack size to unlimited
> setenv STACKSIZE 32768 # limits the stack size to 32M bytes

> ulimit -a # shows the current stack size
> ulimit -s 32768 # sets the stack size to 32M bytes

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.