Monday, May 14, 2012

Dangerous C++ preprocessor

It took me three weeks to find this mistake in a huge code base. Program suddenly crashes after the changes  that look obviously innocent. And the root of all evil was not memory leaks or so but the #ifdef.

header.h

struct mystruct : somebasestruct {
   mystruct();

#ifdef FEATURE_X

int somefield;

#endif

};


module1.cpp

#include ....
#include "header.h"

sizeof(mystruct) = 8

module2.cpp

#include ....
#include "header.h"

sizeof(mystruct) = 4



And guess why? Yes, check building settings, what is defined or not...

No comments: