views:

90

answers:

3

Hi,

I am trying to understand a C code from another programmer. Sometimes I get segmentation faults and I think it could be due to stack overflow. I wonder whether in Visual Studio (or another way) is it possible to estimate in an easy way, given a call to a function, the size it will need on the stack when creating variables.

Thanks

A: 

You can get an estimate of the stack size used by a function by adding together the sizes of the local variables and the sizes used in _alloca() calls.
This is only an estimate, and will typically be a bit larger than the actual stack usage, but it should be enough to get a feeling if you are dealing with a stack overflow.

On the other hand, depending on the compiler settings, the stack will be several kilobytes is size and may even grow as needed.
If, at the point of crash, there really are only a few, small to moderate sized, frames on the callstack, it is far more likely that there is a problem with a pointer access or array index. Be sure to check also that your program does not have a wild pointer overwriting random memory.

Bart van Ingen Schenau
+1  A: 

The size of a frame on the stack is roughly the sum of the sizes of the parameters passed in plus the sum of the sizes of the local variables. There is also a little extra for the return address, base pointer, and perhaps some guard cookies for checking stack overruns.

Example:

int foo(int x, int y, char *string) {
  int array[10000];
  // ...
}

Integers in VC++ are 4 bytes each. A pointer is 4 bytes in a 32-bit executable, or 8 bytes in a 64-bit executable. So the parameters add up to 4+4+8. The local array is 40,000 bytes. This function consumes roughly 40 KB of stack. You could add up a lot of those before you overrun the stack.

An actual stack overflow in a Windows program generally only happens with runaway recursion. The default stack size is pretty large (1 MB). You can get stack corruption by overrunning a buffer that's on the stack.

Chances are your crash has to do with accessing an array beyond its bounds or accessing through a pointer that isn't pointing to valid memory (e.g., a NULL pointer).

Adrian McCarthy
A: 

I think a better approach here is to determine if a stack overflow exception is actually causing the issue. There is a setting in Visual Studio that will cause it to break if a stack overflow occurs. Set this, run the program under the debugger and you will know if a stack overflow is the problem.

  • Debug -> Exceptions
  • Expand Win32 Exceptions
  • Check "Stack Overflow" thrown

alt text

JaredPar
Hi, nice answer. Just antoher question, with VS I develop code for a system that has only 1MB stack size. Can one force VS to have only 1MB stack size so I am working like in the real machine?
Werner
@Werner, I'm not sure that's possible in managed code. But in native code the `CreateThread` function takes an explicit stack size which make work for you. http://msdn.microsoft.com/en-us/library/ms682453(VS.85).aspx
JaredPar