Stackoverflow error at start of program in main function

I made a program, I want to debug it (or run it), and before the first statement in the main function it breaks with the message: Unhandled exception in 0x0020f677 in name.exe: stack overflow. Why is this happening and how to solve the problem? Visual C ++ 2010, the Win32 console application.

EDIT1: The debugger shows me the asm code on chkstk.asm.

What is important for analysis to solve this problem? Is something added to the header files causing this problem?

+6
c ++ stack-overflow
source share
4 answers

If you have reduced the array of a fixed size and if its size is too large, you may get this error.

int fixedarray[1000000000]; 

Try to reduce the length or create it on the heap.

 int * array = new int[1000000000]; 

Remember to delete it later.

 delete[] array; 

But it is better to use std :: vector instead of pointers even in the C function,

 //... int Old_C_Func(int * ptrs, unsigned len_); //... std::vector<int> intvec(1000000000); int * intptr = &intvec[0]; int result = Old_C_Func(intptr,intvec.size()); 

assuming 32 bit compilation.

+5
source share

It sounds like an object constructor or some code called by one causes a stack overflow. I would use a debugger to find out what caused a stack overflow, keeping in mind that the main reason might be the constructor for the global variable.

+3
source share

If the stackoverflow error is caused by deep recursion or large objects on the stack, you can also solve it by increasing the size of the stack. The default size for the stack on Windows is 1MB. You can try to increase it, for example, to 10 MB

Add the following line to the Project → Linker → Command Prompt property

/ STACK: 1,000,000,1000000

If the error is caused by some error in the recursion of the algorithm, increasing the size of the stack does not help, and you need to find an error (possibly in recursion) or an extremely large stack distribution.

0
source share

I had a similar problem, there was no debugging information, there were no global ones, without large arrays.

I had a class with some structures that were # pragma_pack-ed, and if I build this class on the stack, this error occurred. If I put it in a heap, everything will be fine. (any ideas why?)

0
source share

All Articles