What causes a stack overflow?

You might think that it is a coincidence that the topic of my question is similar to the name of the forum, but I really got it here by clicking on the term "stack overflow".

I am using an OPNET network simulator in which I program C. I think I am having a problem with large array sizes. It seems that I find some kind of memory allocation limitation. This may be related to OPNET, Windows, my laptop memory or, most likely, C language. The problem occurs when I try to use nested arrays with a total number of elements that reach several thousand integers. I think I have exceeded the overall memory allocation limit, and I wonder if there is a way to increase this cap. Here is the exact description of the problem:

I basically have a routing table. Let me call it routing_tbl [n], that is, I support 30 nodes (routers). Now for each node in this table, I save the information. about the many (hundreds) of available paths in an array called paths [p]. Again, for each path in this array, I save a list of nodes belonging to it in an array named hops [h]. Thus, I use at least nph integers, and the memory also contains other information. In the same function, I also use another nested array that consumes almost 40,000 integers. As soon as I run the simulation, it stops complaining about the stack overflow. It works when I reduce the overall size of the routing table. What do you think causes the problem and how can it be solved? Very grateful to Ali

+6
c malloc memory stack-overflow
source share
6 answers

This may help if you post the code. Modify the question to include the problem function and error.

Meanwhile, here is a very general answer:

Two main reasons: 1) a recursive function or 2) the distribution of a large number of local variables.

Recursion

if your function calls itself, for example:

int recurse(int number) { return (recurse(number)); } 

Since local variables and function arguments are stored on the stack, then it will populate the stack and cause the stack to overflow.

Large local variables

If you try to allocate a large array of local variables, you can overflow the stack in one simple way. A similar function may cause a problem:

 void hugeStack (void) { unsigned long long reallyBig[100000000][1000000000]; ... } 

There is a fairly detailed answer to this similar question .

+10
source share

Somehow you use a lot of stack. Possible reasons include the fact that you create a routing table on the stack, you pass it to the stack, otherwise you create many calls (for example, recursively processing all this).

In the first two cases, you must create it on the heap and pass a pointer to it. In the third case, you will have to rewrite your algorithm in iterative form.

+3
source share

Stack overflows can occur in C when the number of built-in recursive calls is too large. Perhaps you call a function from yourself too many times?

This error may also be due to the allocation of too much memory in static declarations. You can switch to dynamic allocations via malloc () to fix this problem.

Is there a reason why you cannot use the debugger in this program?

+1
source share

It depends on where you specified the variable.

Local variable (i.e., declared on the stack is limited by the maximum frame size). This is the limit of the compiler that you use (and can usually be configured using compiler flags).

A dynamically allocated object (i.e., the one on the heap) is limited by the amount of available memory. This is an OS property (and can technically increase physical memory if you have a smart OS).

+1
source share

Many operating systems dynamically expand the stack as you use it more. When you start writing to a memory address that is outside the stack, the OS assumes that your stack has only grown a bit and allocates an extra page (usually 4096Kib on x86 - exactly 1024 ints).

The problem is that on x86 (and some other architectures) the stack grows down, but C arrays grow up. This means that if you access the beginning of a large array, you will access memory that is larger than a page away from the edge of the stack.

If you initialize your array to 0, starting at the end of the array (that's right, do a for loop for this), the errors may disappear. If they do, this is really a problem.

You may be able to find some OS API functions for forcing stack distribution or compiler pragmas / flags. I'm not sure how this can be done portable, except, of course, using malloc () and free ()!

+1
source share

You are unlikely to encounter a stack overflow with an overflowing compiled C unless you do something particularly egregious, like runaway recursion or a memory leak. However, your simulator probably has a streaming package that will set stack size limits. When you start a new thread, it allocates a piece of memory for the stack for that thread. There is probably a parameter that you can set somewhere that sets the default stack size, or there may be a way to dynamically grow the stack. For example, pthreads has a function pthread_attr_setstacksize (), which you call before starting a new thread to set its size. Your simulator may or may not use pthreads. Refer to the simulator reference documentation.

0
source share

All Articles