Non-zero default value in constructor [duplicate]
Possible Duplicate:
Undefined / Uninitialized default values in a class
I have a class in C++ that is created and destroyed fairly often. For debugging, I've added a bool mDebug to the class, that I want to turn on with an #ifdef. However, I discovered the messages associated with the debug variable appearing even when the ifdef isn't defined. When I dumped the value in the constructor, I found that it开发者_如何学Go has random values - 35, 68, etc. So I assume that there's some kind of memory leak, and the class is being allocated memory that's already in use. However, I've run purify on it, and it's not showing an uninitialized memory read at that point or any writes out of bounds. valgrind also hasn't been too helpful. For a variety of reasons, gdb doesn't work so well on this code. I can dump the address of the variable, but any other ideas?
Thanks
I'm assuming your "bool mDebug
" added to the class is not a static member, because that would (automatically) be initialized to zero. So, I assume it must be a "regular" data member.
That means you simply did not set its initial value with a "base/member initializer list" or explicitly in the constructor body (either of which you would also wrap in an #ifdef
, since your member definition is wrapped in an #ifdef
).
Members are not set to zero by default (you must do that explicitly). Unless something really strange is going on, I doubt it is a memory leak/error.
精彩评论