Search This Blog

Monday, August 19, 2013

Stack Overrun

In this article I am trying to explain situation which causes stack overrun problem.
I was writing code for a program and was debugging the code. Everything was working properly. I was changing code very speedily. Suddenly my program stopped working due a strange CRASH stating that stack overrun. I was frustrating to find out the reason. I searched on Google but found that the only thing that causing stack overrun was recursive function call. In my case this was not a problem. Debugging also didn’t help. I got some clue because debugger was breaking at assembly code. I went through that assembly file and found that that machine code was probing for the page file. I got another clue after again debugging the code and the clue was that before entering into function the control was calling that assembly code. So here were all mess going on. This code reserve memory to store variable declared in function before entering into that function. In my function there was only one suspicious variable that was a array of bytes 10 MB long. I did this blindly but however I found the actual cause.
 So the conclusion is that don’t allocate very big amount of memory in Stack. Instead of that allocate them in Heap.
Whenever you get this kind of error do 2 things:
1.    Check you are looping in recursive function call.
2.    Check you are not allocating large memory on stack.
If you have more information on this topic please send me. I will update that here.