xiaokaoy
xiaokaoy

Reputation: 1696

Which member of a global union variable that is not initialized explicitly will be initialized to 0 implicitly?

e.g.

union
{
    int n;
    void *p;
} u;

Is the initial value of u.n or that of u.p equal to 0?

It should be noted that a NULL pointer is not necessarily stored in all-zero bits. Therefore, even if u.n and u.p have the same size,

u.n == 0

doesn't guarantee

u.p == 0

and vice versa.

(Sorry for my poor English)

Upvotes: 2

Views: 356

Answers (4)

Shafik Yaghmour
Shafik Yaghmour

Reputation: 158469

Since u is static then the first member will be initialized to zero, from the C99 draft standard section 6.7.8 Initialization paragraph 10:

If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. If an object that has static storage duration is not initialized explicitly, then:

— if it has pointer type, it is initialized to a null pointer;

— if it has arithmetic type, it is initialized to (positive or unsigned) zero;

— if it is an aggregate, every member is initialized (recursively) according to these rules;

if it is a union, the first named member is initialized (recursively) according to these rules.

since n is a arithmetic type it will be initialized to zero. The value of p is unspecified but in practice type punning is usually supported by the compiler for example the gcc manual points here for Type-punning and we can see under -fstrict-aliasing section is says:

The practice of reading from a different union member than the one most recently written to (called “type-punning”) is common. Even with -fstrict-aliasing, type-punning is allowed, provided the memory is accessed through the union type.

It is also worth noting that you may be able to initialize any member of a union like so:

union { int n; void *p; } u = { .p = NULL } ;
                              ^^^^^^^^^^^^^

I am not sure if all compilers support this though.

Upvotes: 1

user2776886
user2776886

Reputation: 1

It depends upon how you instantiate the variable itself. Usually static defined variables are initialized to zero. If you malloc the union to a pointer, you may get uninitialized memory. However, if you use calloc to allocate memory for the union, calloc will initialized the allocated memory to zero per the man page.

It may also depend on any libraries that you may be using. Google's perftools library may or may not zero out the memory when you make the call to calloc that it overwrites.

Upvotes: -1

Dayal rai
Dayal rai

Reputation: 6606

if Object with Static storage duration is not initialized explicitly, then:

— if it has pointer type, it is initialized to a null pointer;

— if it has arithmetic type, it is initialized to (positive or unsigned) zero;

— if it is an aggregate, every member is initialized (recursively) according to these rules;

— if it is a union, the first named member is initialized (recursively) according to these rules.

So u.n will be initilaized to zero and u.p is undetermined.

EDIT: Response to comment

above info copied from ISO/IEC 9899:201x 6.7.9.10

Upvotes: 3

AnotherSmellyGeek
AnotherSmellyGeek

Reputation: 518

I think by 'global variable' you mean that it's at file scope. If so, and if it's declared 'static', it will be initialized to all zero bits, eg because it gets allocated in .BSS. What those zero bits in the union's storage mean in terms of the value of whichever of its members you access depends on their types. In your case, all zero bits in an int means it has the value zero, and all zero bits in a pointer makes it NULL, ie @Dukeling's bang on here. I am not sure that all zero bits in a float would yield a float with value zero.

Upvotes: -1

Related Questions