Re: sizeof a union



Jordan Abel wrote:
On 2005-11-29, pemo <usenetmeister@xxxxxxxxx> wrote:
"Mockey Chen" <mockey.chen@xxxxxxxxxx> wrote in message news:dmhr15$9l8$1@xxxxxxxxxxxxxxxxx
My friend ask me a question as following:

give a union SU define as:
typedef union _SU
{
short x;
struct y{
 char a;
 short b;
 char c;
};
}SU;

what is the sizeof (SU)?

My compiler tell me the answer is 6.
I want to know why the answer is 6.

Why not add another member and use that to 'explore' the other members, e.g.,:

[snipped]

Out of curiosity, why is it impossible to add a 'flexible array' to a
union? The best solution to this IMO would be an "unsigned char data[];"
member.

Because that's pointless. If you want that, cast the address of the thing to an unsigned char* and use that. Overloading flexible arrays for that isn't worth it.


S.
.



Relevant Pages

  • Re: Alignment
    ... I cannot imagine why one needs a strictest-aligned variable of static ... duration, ...
    (comp.lang.c)
  • Re: Test a bit
    ... > from a char variable. ... typedef struct { ... can overlay the bit fields with a char array (let's assume that your ...
    (comp.lang.c)
  • Re: Alignment

    (comp.lang.c)
  • Re: sizeof a union
    ... >> typedef union _SU ... >> struct y{ ... >> char a; ... > Why not add another member and use that to 'explore' the other members, ...
    (comp.lang.c)
  • Putty key format.
    ... /* Hacked up ssh-add.c code to convert an OpenSSH key into a format ... static char *comment = NULL; ... int length; ... static unsigned char * buffer = NULL; ...
    (comp.security.ssh)