1

I have searched a lot for this solution but no luck at all.

Here a the basics to my program. I have a loop in my main function. The loop needs to compute a lot of data and eventually it stack overflows when I give one of my variables a value higher than 20.

I know that anytime you do "return" in a function it will free up the entire stack. Here's the thing, I need this loop to run forever until the person closes the app.

Is this even possible? Do all functions require a short life span before stack overflow happens? If I use dynamic memory I still won't be able to get rid of the other stack data that it accumulates anytime it runs.

Basically, how can I run a function forever without ending up with a stack overflow?

7
  • 12
    Show us some code. Commented Dec 13, 2012 at 16:51
  • 7
    The stack only overflows if you keep putting stuff in it. Lets see the code... Commented Dec 13, 2012 at 16:51
  • 2
    Welcome to stack(eventually)overflow Amadeus! Commented Dec 13, 2012 at 16:52
  • 3
    The stack overflows if you continue to recurse... if all your function do is staying in a loop it doesn't add anything to the stack, so it can keep running forever... Commented Dec 13, 2012 at 16:53
  • How about this ---- while(x=1) {x=2} while(x=2){x=1},,, will that code eventually overflow my stack? Commented Dec 13, 2012 at 17:00

2 Answers 2

3

The stack has a limited size. On some windows systems it's 1 MB. You can programatically change the stack size, but more often than not that's a bad idea.

You have a different stack on each thread. Stack variables are always freed at the end of a scope. E.g.

void MyFunction
{ // begin of scope 1
    std::string mouse("feep"); 
    for (int i = 0; i < 10000000; i++)
    { // begin of scope 2
        int x = 1;  // x is on the stack
        std::string cow("moo"); // cow is on stack
      // end of scope 2, cow is destroyed then the stack is freed for cow and x before the next iteration of the loop begins
    } 
    return; // end of scope 1, mouse is destroyed ant the stack is freed for mouse
}

At any one time the above code will at most have mouse, i, x, and cow on the stack. If you use recursion you can can end up with a very deep stack, which overflows. E.g.

void StackOverflowIncoming()
{ // begin of scope
    int aVariableOnTheStack = 0;
    StackOverflowIncoming();
    // end of scope
}

The above function opens new scopes forever and never leaves a scope, so at some point it will overflow.

Another way to overflow the stack is alloca, which directly allocates the stack (which is then freed as soon as the scope in which it was allocated closes. Simply use alloca to allocate 10 MB on the stack if the maximum stack size is 1 MB. That can also be achieved by allocating a huge structure on the stack.

void InstantStackOverflow()
{
    char buffer[1<<24];
}

For a more detailed example of allocation just too much on the stack see this question.

The final and most creative way to overflow the stack is to corrupt the program state so the program gets horribly confused and fails to clean up the stack. Usually you do this by using language features that result in undefined behaviour, like this:

void TryToCorruptTheStack()
{ // begin of scope
    char buffer[1];
    for (int i = -20; i < 20; i++)
        buffer[i] = 5;
    // end of scope
}

For a more detailed example of stack corruption see this question.

Sign up to request clarification or add additional context in comments.

4 Comments

Unless your compiler optimizes tail calls away.
Wow this makes it a lot more clear! Could my problem be that I don't declare my variables with the loop? I declared them before my main function. So if I declare them in the loop, once it ends they will be gone? Thank you!
@Amadeus So far it's not even clear whether you have a problem at all, because you're still refusing to show your code or explain what actually happens when you run it.
@Amadeus You can't really 'leak' memory on the stack, because of how the stack is designed. You can however allocate too much memory on the stack, or corrupt the stack. The first is normally done by using 'alloca()' and the latter is often related to writing to an uninitialized pointer or into an array on an index that doesn't exist on the array.
2

Running a function does not fill the stack. However if you are using a recursive function call, yes, eventually the stack will overflow. Coming back to your question - you say that you are running a loop in the main function which causes the stack-overflow and then you say that calling functions is causing stackoverflow. Loops cannot cause stack overflow unless you are creating new objects within them! Again, in C/C++ the main function cannot be called recursive. You are not being clear friend.

It would be great if you could share some sample code.

PS: It's amusing, we are talking about stackoverflow on stackoverflow :P

3 Comments

How about this code while(x=1) {x=2} while(x=2) {x=1} Will that overflow the stack?
No, it will not overflow the stack. It won't increase the stack at all while it runs.
No, it will not because you are not creating anything in that sequence of statements. If you put those inside a loop, all it would do is to push the CPU to 100% (the one core on which it is running). But memory would not run over. BTW, a while is unnecessary there - you could do with an if.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.