c++debuggingreleasevisual-c++-2008

How does debug differ from release executable?


I had a programming error like this in malloc's size:

buffer = (char *) malloc(cbHash + 1);
assert(buffer);

instead of:

buffer = (char *) malloc(cbHash * 2 + 1);
assert(buffer);

I was writing two times more in buffer but in debug mode it did worked fine but on release it did crash in assert, that's how I figured out my mistake. My question is: how does debug differ from release executable? and why I didn't get error in debug mode?


Solution

  • Because you were writing still in application's memory space. Also writing outside of allocated memory results in an Undefined Behaviour, not necessarily a crash. Writing outside of app's memory results in a crash, because system sees that application violates memory of other program and violation access error is issued. However if you stay within app's memory, system cannot tell anything, and programs enters the UB state.

    That is a short basic explanation. There is a little bit more to it. If you are interested I suggest looking into address spaces and memory access rights.

    Also debug mode often does not have any optimizations and contains debug symbols as opposed to release builds. They might contain symbols, but often don't.

    The assert could fail only if malloc failed for some reason.