Initializing unions

Initializing unions

Post by Jack Klei » Wed, 11 Oct 2006 12:41:45


Consider the following:

union u {
unsigned short us;
unsigned long ul;
};

And let's assume that sizeof(short) < sizeof(long).

Now given the following snippet:

union u extern_u;

int main(void)
{
static union u static_u;
union u auto_u = { 0 };
}

Am I correct in assuming that I cannot depend on the .ul member being
necessarily initialized to 0L in any of these cases? I certainly
think so, but I wouldn't mind if someone could quote chapter and verse
to prove me wrong.

--
Jack Klein
Home: http://www.yqcomputer.com/
FAQs for
comp.lang.c http://www.yqcomputer.com/
comp.lang.c++ http://www.yqcomputer.com/ ++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.yqcomputer.com/ ~ajo/docs/FAQ-acllc.html
 
 
 

Initializing unions

Post by Jun Woon » Wed, 11 Oct 2006 21:26:11


Yes. Even when the .ul member has the same size as the .us member,
you cannot depend on it if the .ul member contains a padding bit,
which is a rare case as the standard notices.


``All opinions expressed are mine, and do not represent
the official opinions of any organization.''

 
 
 

Initializing unions

Post by Jun Woon » Wed, 11 Oct 2006 21:44:36


[...]

Oops, forget the "as the standard notices" phrase. I was confused
with the "memsetting an integer object with 0x00" case.


``All opinions expressed are mine, and do not represent
the official opinions of any organization.''