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:
s开发者_运维百科truct 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()?
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 toT
— 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.
To the proper null pointer value. See 8.5/5
- if T is a scalar type [and a pointer is a scalar type, see 3.9/10] the object is set to the value of 0 (zero) converted to T
精彩评论