Why does stack overflow not cause an error in Visual C ++?

In Microsoft Visual C ++ 2010, I created a program that leads to a stack overflow. When I run the program using "start debugging", an error occurs when the stack overflows. When I run it with a "start without debugging", an error does not occur, and the program simply ends silently, as if it had been successfully completed. Can someone explain to me what is going on? Also, don't any other compilers make stack overflow errors?

(I thought this would be a good place to ask a stack overflow question.)

+4
source share
6 answers

C ++ will not hold your hand as a managed environment. Presence means undefined behavior.

+9
source

Stack overflow is undefined behavior. The compiler has the right to ignore it or cause any event.

+5
source

Because when your process stack overflows, it is no longer a valid process. Displaying an error message requires a stack.

Raymond Chen moved on to this recently.

Due to why the debugger is able to create such an exception, in this case the process is supported, since it is connected in debug mode to the debugger process. Your process does not display an error, debugger.

On Windows computers, you can catch a SEH exception that corresponds to a stack overflow. For example, you can see the source code of boost :: regex (Google for BOOST_REGEX_HAS_MS_STACK_GUARD).

+4
source

It is possible that the compiler has optimized the alleged stack overflow. Consider the following pseudo-code example:

void RecursiveMethod(int n) { if (n % 1024 == 0) print n; // call recursively RecursiveMethod(n + 1); } 

The method will call itself recursively and overflow the stack quite quickly because there are no exit conditions.

However, most compilers use tail recursion , a method that will pass a call to a recursive function into the build loop.

It should be noted that with tail recursion, the above program will work in an infinite loop and will not exit silently.

Bart de Smet has a good blog article explaining how this technique works in .NET:

Case of failed demonstration - StackOverflowException on x64

+2
source

In debug mode, you need overhead. You want it to detect that you have broken your stack, overflowed your buffers, etc. This overhead is built into the debugging tools and the debugger. In high-level terms, debugging equipment is additional code and data placed there to help flag errors, and the debugger detects the errors noted and notifies the user (in addition to helping you debug, of course).

If you are working with a project compiled in release mode or without a connected debugger, there is no one to hear the cry of your program when it dies :) If the tree falls into the forest ...

Depending on how you program, C ++ programs without preparing the wheels. If you hit a wall, nobody will be there to tell you that you squinted. You just crash and burn, or, even worse, crash and continue to run in a very distorted state, not knowing that something is wrong. Because of this, it can be very fast. There are no unnecessary checks or protective fences to prevent it from breaking through your program at full speed and processor potential (and, of course, how many additional steps you encoded in your program).

+1
source

The debug build runs a series of stack checks to help you detect issues such as stack overflow, stack corruption, etc. They are not present in release builds because it will affect application performance. As others have noted, stack overflows are undefined behavior, so the compiler does not need to perform such stack checks at all.

When you are in a debugging environment, runtime checks will help you detect problems that may also occur in your version of the assembly, so if you fix all the problems found in your debug assembly, then they should also be fixed in your release., In theories. In practice, sometimes the errors that you see in your debug assembly are missing from your version of the assembly, or vice versa.

Stack overflow should not occur. As a rule, a stack overflow occurs only when an unintended recursive function is called or by allocating a sufficiently large buffer on the stack. The former is obviously a mistake, and the latter should use a bunch.

+1
source

All Articles