0
#include <stdio.h>

typedef struct
{
    int a;
    char c;
    int d;

 }t;

 typedef struct
 {
      int a;
      char c;
      char d;
      char e;
      int f;

 }t1;

int main()
{
    printf("sizeof t = %d", sizeof(t));
    printf("sizeof t1 = %d", sizeof(t1));
}

Output:

sizeof t = 12
sizeof t = 12

Without using #pragma pack(), sizeof operator is returning same value for two different structure. Could you please explain?

Eswaran Pandi
  • 602
  • 6
  • 10
  • 1
    Padding/alignment, probably. See [this question](https://stackoverflow.com/questions/119123/why-isnt-sizeof-for-a-struct-equal-to-the-sum-of-sizeof-of-each-member) – Chris Sprague Aug 11 '17 at 14:00
  • `int` has sizeof 4 and alignof 4 on your machine. – Antti Haapala -- Слава Україні Aug 11 '17 at 14:01
  • See also this question: [Structure padding and packing](https://stackoverflow.com/questions/4306186/structure-padding-and-packing) – lurker Aug 11 '17 at 14:01
  • I am surprised though that the 3 chars in the second struct are actually packed. – Eugene Sh. Aug 11 '17 at 14:01
  • structs have to be aligned to the lowest common multiple of their members' alignment. Assuming an `int` is 4 bytes and a `char` is 1, `t` is 9 bytes aligned to the nearest multiple of 4 = 12 bytes and `t2` is 11 bytes aligned to the nearest multiple of 4 = 12 bytes. – Elan Hamburger Aug 11 '17 at 14:02
  • @ElanHamburger no. `t` is 12 bytes aligned to 12. if `char` came last only then there would be padding bytes at the end. – Antti Haapala -- Слава Україні Aug 11 '17 at 14:02
  • @AnttiHaapala 4 + 1 + 4 = 9? – Elan Hamburger Aug 11 '17 at 14:04
  • @ElanHamburger The members are not reordered. `int; char; int; char;` would have sizeof 16 on this computer. Though the sum of sizeofs of members is 10. – Antti Haapala -- Слава Україні Aug 11 '17 at 14:04
  • @AnttiHaapala but that's due to the alignment requirements of the struct. 9 bytes are in use, 3 bytes are padding due to the struct. – Elan Hamburger Aug 11 '17 at 14:05
  • 1
    Just for completeness: `sizeof` is not a function, it's an operator. –  Aug 11 '17 at 14:06
  • @ElanHamburger a `struct` doesn't in itself have any alignment requirements. Only the members have. `t` is 9 bytes aligned to the nearest multiple of `12` is wrong. It is the `a` and `d` that have the alignment requirements. – Antti Haapala -- Слава Україні Aug 11 '17 at 14:08
  • 1
    In addition, don't print size_t with the %d format specifier, it invokes undefined behavior. Use %zu instead. – Tom Kuschel Aug 11 '17 at 14:12
  • @AnttiHaapala You're right that in this case the alignment is due to the members' alignment requirements (I phrased my initial comment incorrectly), but structs absolutely have their own alignment requirements (what if you wanted an array of structs?). – Elan Hamburger Aug 11 '17 at 14:13
  • @AnttiHaapala ["Note that the alignment of any given struct or union type is required by the ISO C standard to be at least a perfect multiple of the lowest common multiple of the alignments of all of the members of the struct or union in question."](https://gcc.gnu.org/onlinedocs/gcc-3.3/gcc/Type-Attributes.html) – Elan Hamburger Aug 11 '17 at 14:15
  • See stack Doc on [structure padding and packing](https://stackoverflow.com/documentation/c/4590/structure-padding-and-packing) – EsmaeelE Aug 11 '17 at 20:20

0 Answers0