4

I was asked today in an interview to list four differences that may occur between debug and release builds. I suppose they meant differences in behavior and not differences such as debug information and whatnot. I was only able to name two:

  1. Debug builds are generally much slower due to some functions not being inlined.
  2. Due to the difference in speed, in multi-threaded programs that have race conditions, these race conditions may become apparent in only one of the two builds.

What other differences could I have named?

Paul Manta
  • 30,618
  • 31
  • 128
  • 208

3 Answers3

7

Here's a summary of a few differences: http://msdn.microsoft.com/en-us/library/aa236698%28v=vs.60%29.aspx. It includes:

  • heap layout (the use of a debugging memory allocator)
  • macros (including assert statements, as well as anything included in "#ifndef NDEBUG", which can be substantial differences in some cases -- e.g., I know that some boost libraries add extra fields to structs when compiled in debug mode, which can be used to perform sanity checks)
  • optimizations (mostly disabled in debug builds)
  • initialization & bad pointers: uninitialized variables have undefined state until you assign to them; but in debug builds, they'll often be initialized to some known state (eg all zero or all #CCCCCCC etc).
Edward Loper
  • 15,374
  • 7
  • 43
  • 52
3

Besides your two answers, here's another four:

  1. _DEBUG vs NDEBUG
  2. The linker uses different libraries during debug and release
  3. The debug build also produces the symbols used for debugging
  4. Code can end up optimized away, so, in certain situations, you may get fewer calls to some constructors during release, which can cause some nasty bugs. here's an example:
Object x(3);
Object y;
y = x;

versus:

Object x(3);
Object y = x;
Community
  • 1
  • 1
Mihai Todor
  • 8,014
  • 9
  • 49
  • 86
1

Another point is that many libraries have additional checks enabled with debugging. This may ironically mean that code works in debug build, but no in release build:

Imagine an allocation function that zeros out memory in debug build but not in release build due to performance reasons. If the value of this variable is subsequently read without being initialised, the debug build sees a well-defined (zero) value while the release build can see any value.

Conversely, debug build checks can of course catch undefined behaviour e.g. by checking that an access to [] falls within the defined range.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214