Lung.S.Wu <> wrote:
> From ANSI C definition, section 6.7.8, with UNION, it says that only
> the first named element will be initialized if it is not initialized
> explicitly.
> That means, such the code
> 1 typedef union {
> 2 char a;
> 3 short b;
> 4 int c;
> 5 } UU;
> 6
> 7 UU u;
> 8
> 9 int main(void)
> 10 {
> 11
> 12 return 0;
> 13 }
> Only the member u.a is initialized as 0, And others are undefined.
> But from modern system design (ELF), all memory of the global variable
> u will be cleared to 0. Because u is saved at BSS, and program loader
> will clear BSS segment after a executable file is load into main memory.
That may be the case for a number of systems, but the C standard
isn't made for a sub-class of the exising systems, it tries to
address, as far as possible, all kinds of systems.
> My question...
> 1. Does exist any system that will not clear remain elements of global
> union variable?
> 2. Why ANSI C org. will not define all union members be cleared to zero?
There are definitely systems where e.g. a floating point value
or a NULL pointer has a representation that's not all-bits-zero.
If on such a system you have a union that contains one of those
as well as a member that has an all-bit-zero representation it
would be impossible to initialize both members at the same time
to a "zero" value. Thus it would be counterproductive if the C
standard would request that more than a single element is initi-
alized to a "zero" value since it would make it impossible to
write a conforming compiler for such a system
Regards, Jens
--
\ Jens Thoms Toerring ___
\__________________________
http://toerring.de