user478571
user478571

Reputation:

Address Alignment for Union

I am a bit confused about Memory address alingment for union.

typedef union { 
               char state[x];
               char encode[4];
               int index;
} Location;

We use several alignment rule while writing code, ex:

int      4 bit alignment
double   4 bit for linux
char *   4 bit for linux

"how that union will be aligned" For those conditions; If

   x < 4      ?
   x = 4      ?
   x > 4      ?

platform linux assembly x86 64 bit

NOTE: If you have better opinion, feel free when improve that question

Upvotes: 0

Views: 190

Answers (1)

paxdiablo
paxdiablo

Reputation: 881403

I'm a bit confused as to what your actual question is :-)

But, if you're asking how that union will be aligned, a union has to follow two relevant rules here.

  1. All items must start at the start of the union.
  2. The union as a whole must have the alignment of its strictest component.

In other words, it's likely that the int is the driving force here in terms of alignment

The value of x is irrelevant here if the alignment of a single char is one. It's not the whole array that needs to be aligned, just each element of it.

Upvotes: 1

Related Questions