469,621 Members | 1,678 Online

# Address of union members

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
Jul 22 '05 #1
6 3097
Ioannis Vranos <iv*@guesswh.at.grad.com> wrote in news:cbusib\$1684\$1
@ulysses.noc.ntua.gr:
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".
Jul 22 '05 #2
Ioannis Vranos wrote:

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
Jul 22 '05 #3

"Ioannis Vranos" <iv*@guesswh.at.grad.com> wrote in message
news:cb***********@ulysses.noc.ntua.gr...
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
Jul 22 '05 #4
Mike Wahler wrote:
"Ioannis Vranos" <iv*@guesswh.at.grad.com> wrote in message
news:cb***********@ulysses.noc.ntua.gr...
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.

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?
Jul 22 '05 #5
In article <cb***********@news.rchland.ibm.com>,
Bill Seurer <se****@us.ibm.com> wrote:
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
Jul 22 '05 #6
"Bill Seurer" <se****@us.ibm.com> wrote in message
news:cb***********@news.rchland.ibm.com...
Mike Wahler wrote:
"Ioannis Vranos" <iv*@guesswh.at.grad.com> wrote in message
news:cb***********@ulysses.noc.ntua.gr...
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.

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
Jul 22 '05 #7

### This discussion thread is closed

Replies have been disabled for this discussion.

### Similar topics

 5 posts views Thread by Simon Elliott | last post: by 6 posts views Thread by Neil Zanella | last post: by 26 posts views Thread by Adam Warner | last post: by 18 posts views Thread by ranjeet.gupta | last post: by 14 posts views Thread by Clint Olsen | last post: by 16 posts views Thread by tedu | last post: by 4 posts views Thread by Michael Brennan | last post: by 32 posts views Thread by =?gb2312?B?zfWzrLey?= | last post: by 15 posts views Thread by Pengjun Jia | last post: by reply views Thread by billypeterson | last post: by reply views Thread by eddparker01 | last post: by reply views Thread by eddparker01 | last post: by reply views Thread by devrayhaan | last post: by reply views Thread by neerajsundriyal | last post: by 1 post views Thread by Simmer | last post: by reply views Thread by gheharukoh7 | last post: by reply views Thread by tieutu2004 | last post: by reply views Thread by aprens | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.