K
Koen
Hi!
Does anyone know what the standard says about the way unions are
stored in C? I mean the following:
Let's say you have a union with a double and a char field:
union MyUnion_t
{
double Double;
char Char;
};
MyUnion_t aUnion;
Is it standardized somehow which byte of the allocated storage the
Char field will use?
And a related question: if you dump unions in binary form to a file,
and then reload them from the file on a different platform, or with a
program compiled by a different compiler, are you guaranteed to get
back what you stored? (I think not, but I'm not sure)
And another point: if you use the same union across different ABI's,
will that work without problems? For example: if you get a pointer to
such a union from a library compiled by a specific compiler, and then
use it in another module compiled with a different compiler, will
aUnion.Char work as expected?
Someone on the comp.std.c group found this:
That doesn't seem to guarantee much does it?
I was hoping to use (*ptr).Char or (*ptr).Double without having to cast
anything. Looks like no more details are specified about whether this is
supposed
to work across different ABI's:
MyUnion_t theUnion;
somelib->getUnion(&theUnion);
where this is in my own program, and somelib is a function table into a
library possibly compiled by another compiler.
Will the libary return a union with the same binary layout as my own
program would do?
Koen
Does anyone know what the standard says about the way unions are
stored in C? I mean the following:
Let's say you have a union with a double and a char field:
union MyUnion_t
{
double Double;
char Char;
};
MyUnion_t aUnion;
Is it standardized somehow which byte of the allocated storage the
Char field will use?
And a related question: if you dump unions in binary form to a file,
and then reload them from the file on a different platform, or with a
program compiled by a different compiler, are you guaranteed to get
back what you stored? (I think not, but I'm not sure)
And another point: if you use the same union across different ABI's,
will that work without problems? For example: if you get a pointer to
such a union from a library compiled by a specific compiler, and then
use it in another module compiled with a different compiler, will
aUnion.Char work as expected?
Someone on the comp.std.c group found this:
C99 6.7.2.1p14 says:
The size of a union is sufficient to contain the largest of its
members. The value of at most one of the members can be stored in
a union object at any time. A pointer to a union object, suitably
converted, points to each of its members (or if a member is a bit-
field, then to the unit in which it resides), and vice versa.
That doesn't seem to guarantee much does it?
I was hoping to use (*ptr).Char or (*ptr).Double without having to cast
anything. Looks like no more details are specified about whether this is
supposed
to work across different ABI's:
MyUnion_t theUnion;
somelib->getUnion(&theUnion);
where this is in my own program, and somelib is a function table into a
library possibly compiled by another compiler.
Will the libary return a union with the same binary layout as my own
program would do?
Koen