Address of union members

D

Denis Remezov

Ioannis said:
In the union

union test { int i; int j; }a;

Is there any guarantee that a.i and a.j share the same memory address?

Pointers to data members of the same union object compare equal [5.9/2].
Therefore, they represent the same address [5.10/1].

(A separate permission to inspect the common initial sequence [9.2],
I assume, would not be sufficient to guarantee that).

Denis
 
I

Ioannis Vranos

In the union



union test { int i; int j; }a;


Is there any guarantee that a.i and a.j share the same memory address?
In the standard it is mentioned:


"The size of a union is sufficient to contain the largest of its data
members. Each data member is allocated as if it were the sole member
of a struct."


The key here is "sufficient to contain the largest of its data members"
which leaves the case open to be larger than the largest member, so for
example in an implementation it could be even:


sizeof(test)==2*sizeof(int);






Regards,

Ioannis Vranos
 
A

Andre Kostur

In the union



union test { int i; int j; }a;


Is there any guarantee that a.i and a.j share the same memory address?
In the standard it is mentioned:


"The size of a union is sufficient to contain the largest of its data
members. Each data member is allocated as if it were the sole member
of a struct."


The key here is "sufficient to contain the largest of its data members"
which leaves the case open to be larger than the largest member, so for
example in an implementation it could be even:


sizeof(test)==2*sizeof(int);

It seems that the Standard supports that i and j are at the same location.
See section 9.5(1) and 9.5(2) where it talks about each member being
allocated as it if was the sole member of a struct. And in (2) it shows an
example union with an int a, and char* p, and it mentions "but since they
are union members they have the same address".
 
M

Mike Wahler

Ioannis Vranos said:
In the union



union test { int i; int j; }a;


Is there any guarantee that a.i and a.j share the same memory address?
Yes.

In the standard it is mentioned:


"The size of a union is sufficient to contain the largest of its data
members. Each data member is allocated as if it were the sole member
of a struct."

That right there implies all members have the same address.
The key here is "sufficient to contain the largest of its data members"
which leaves the case open to be larger than the largest member,

Yes, but this affects the members' addresses not at all.
so for
example in an implementation it could be even:


sizeof(test)==2*sizeof(int);

Yes, it could, because of allowed 'padding'. But both
members 'i' and 'j' will have the same address, and
the same size (they have the same type).

-Mike
 
B

Bill Seurer

Mike said:
That right there implies all members have the same address.

Does it? Does the first member of a struct always have the same address
as the whole struct itself? That would seem to be the case but is it
guaranteed?
 
C

Chad J McQuinn

Bill Seurer said:
Does it? Does the first member of a struct always have the same address
as the whole struct itself? That would seem to be the case but is it
guaranteed?

9.2.17 A pointer to a POD struct object, suitably converted using a
reinterpret_cast, points to its initial member (or if that member is a
bitfield, then to the unit in which it resides) and vice versa. [Note:
There might therefore be unnamed padding within a POD struct object, but
not at its beginning, as necessary to achieve appropriate alignment.]

-Chad
 
M

Mike Wahler

Bill Seurer said:
Does it? Does the first member of a struct always have the same address
as the whole struct itself? That would seem to be the case but is it
guaranteed?

Yes.

9.2 / 17

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top