Search code examples
c++standards-compliancelanguage-lawyer

Are uninitialized pointers in objects with static storage duration initialized to NULL, or to all-zeros?


out of curiousity and because I don't have my copy of the standard at hand right now:

Given an implementation where null pointers are not represented by an all-zeros pattern, will uninitialized pointer members of objects with static storage duration be initialized to the proper null pointer value, or to an all-zeros value?

Less standardese, more code:

struct foo {
    void *p;
};

foo f;

Given a NULL pointer representation of 0x00000001, what can I expect for the bitwise representation of f.p at the beginning of main()?


Solution

  • The standard says (8.5/4):

    To zero-initialize an object of type T means:

    — if T is a scalar type, the object is set to the value 0 (zero), taken as an integral constant expession, converted to T

    — if T is a non-union class type, each non-static data member and each base-class subobject is zero-initialized;

    So f is effectively initialised as f = { (void *)0 }, and we know from 4.10/1:

    A null pointer constant is an integral constant expression rvalue of integer type that evaluates to zero. A null pointer constant can be converted to a pointer type; the result is the null pointer value of that type

    So you will get the correct NULL value.