0

Assume that i have a structure which is defined as shown below:

 typedef struct
 {
   char a;
   int b;
   char c;
 }abc_t;

Now as per the rules of padding, character variable could start at any address since it has only a single byte while intger variable should start at an address which is divisible by 4 while short variable should start at any even address. in that case if we assume that character variable starts at OFFSET 0.

struct
{
    char a; // OFFSET 0+3 bytes padding
    int b;  // OFFSET 4
    char c; //OFFSET 5+3 bytes padding
}abc_t;

Here the total size of structure would become 12.

But my doubt is if the first element of a structure which is 'char a' here starts at an offset 1 , then based on the rules of padding, we would have only 2 bytes padded after a and hence the size of structure would be 8 bytes.

struct
{
    char a;//OFFSET 1+2 bytes
    int b;//OFFSET 4
    char c;//OFFSET 8
}abc_t;

Same would be the case of any structure variable which would start with short variables. Can you please tell me if my understanding regarding this is correct or can we safely assume that first member of any structure would always start at an address which is divisible by 4?

Thanks a lot in advance.

Stacky
  • 1
  • 1
  • The structure shown will start on a multiple of 4 bytes under reasonably common strictures/assumptions. There cannot be leading padding in a structure. Other structures might need to be more or less stringently aligned. For example, `struct ver1 { char a; short b; char c; }` might well be size 6 and aligned on any even boundary, while `struct ver2 { char a; double d; char e; }` might well be size 24 and aligned on any multiple of 8 bytes. – Jonathan Leffler Oct 23 '16 at 04:09
  • I'm pretty sure there's a rule that the address of the first member of a structure is always numerically equal to the address of the structure, i.e. there is never any padding before the first member, i.e. the offset of the first member is always 0. – Steve Summit Oct 23 '16 at 04:10
  • @SteveSummit: Definitely: §6.7.2.1 **Structure and union specifiers** ¶15 _… There may be unnamed padding within a structure object, but not at its beginning._ …¶17 _There may be unnamed padding at the end of a structure or union._ – Jonathan Leffler Oct 23 '16 at 04:13

1 Answers1

-1

There are a few issues here:

1) Size of int is not necessary 4 bytes, its sizeof(int) which is compiler defined. Can be 4 bytes, 8 bytes or even 2 bytes depending on word length. See Is the size of C "int" 2 bytes or 4 bytes?.

2) Field alignment in a struct is up to the compiler. The order in which you put the fields doesn't guarantee tighter packing. The order of fields is preserved, however. See Why isn't sizeof for a struct equal to the sum of sizeof of each member?.

3) Putting shorter structs together would USUALLY cause tighter packing.

4) Structs begin at word boundaries. There is never any padding before the first field. So yes, the address would be at the beginning of a word. Why use address of first element of struct, rather than struct itself?.

Hope this helps.

Community
  • 1
  • 1
Proton14
  • 11
  • 1