C++: member pointer initialised?

c++, default, pointers

Solution

Here is the relevant passage fromt he standard:

12.6.2 Initializing bases and members [class.base.init]

4 If a given nonstatic data member or base class is not named by a mem- initializer-id in the mem-initializer-list, then

--If the entity is a nonstatic data member of (possibly cv-qualified) class type (or array thereof) or a base class, and the entity class is a non-POD class, the entity is default-initialized (dcl.init). If the entity is a nonstatic data member of a const-qualified type, the entity class shall have a user-declared default constructor.

--Otherwise, the entity is not initialized. If the entity is of const-qualified type or reference type, or of a (possibly cv-quali- fied) POD class type (or array thereof) containing (directly or indirectly) a member of a const-qualified type, the program is ill- formed.

After the call to a constructor for class X has completed, if a member

of X is neither specified in the constructor's mem-initializers, nor default-initialized, nor initialized during execution of the body of the constructor, the member has indeterminate value.

Problem

Code sample should explain things: ``` class A { B* pB; C* pC; D d; public : A(int i, int j) : d(j) { pC = new C(i, "abc"); } // note pB is not initialised, e.g. pB(NULL) ... }; ``` Obviously pB should be initialised to NULL explicitly to be safe (and clear), but, as it stands, what is the value of pB after construction of A? Is it default initialised (which is zero?) or not (i.e. indeterminate and whatever was in memory). I realise initialisation in C++ has a fair few rules. I think it isn't default initialised; as running in debug mode in Visual Studio it has set pB pointing to 0xcdcdcdcd - which means the memory has been new'd (on the heap) but not initialised. However in release mode, pB always points to NULL. Is this just by chance, and therefore not to be relied upon; or are these compilers initialising it for me (even if it's not in the standard)? It also seems to be NULL when compiled with Sun's compiler on Solaris. I'm really looking for a specific reference to the standard to say one way or the other. Thanks.

Original source