Address of union members

From: Ioannis Vranos (ivr_at_guesswh.at.grad.com)
Date: 06/30/04


Date: Wed, 30 Jun 2004 20:17:11 +0300

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



Relevant Pages

  • Re: Undefined behaviour
    ... if a member of a union object is accessed after ... a common initial sequence if corresponding members have compatible ... The C99 standard says virtually the same thing. ...
    (comp.lang.cpp)
  • Re: Warning comes when using Unions
    ... I have declared an Union as follows ... member of a structure with more than one named member may have incomplete ... The expression that specifies the width of a bit-field shall be an integer ... declarator. ...
    (comp.lang.c)
  • ATI driver compilation problem
    ... I tried the ATI driver as supplied by ATI and the one supplied by livna ... member `pgd' in something not a structure or union ... defaults to `int' in declaration of `drm_agp_module_stub' ...
    (Fedora)
  • Ati fglrx driver & cko1 ?
    ... argumento 1 de `pmd_offset' de tipo de puntero incompatible ... to `int' in declaration of `drm_agp_module_stub' ... member `free_memory' in something not a structure or union ...
    (Debian-User)
  • Ati fglrx driver & 2.6.11 kernel?
    ... Has any of you successfully compiled fglrx driver under 2.6.11 kernel? ... to `int' in declaration of `drm_agp_module_stub' ... member `free_memory' in something not a structure or union ...
    (Debian-User)