0

For the below program, I was expecting the sizeof(node1) and sizeof(node2) to be 12 bytes on a 64 bit platform. But it seems that sizeof(node1) = 12 and sizeof(node2) = 16. Why the difference?

#include <stdio.h>
struct node1
{
    int a;
    int b;
    int c;
};
struct node2
{
    int* a;
    int c;
};
int main()
{
    printf("size1 = %d, size2 = %d\n", sizeof(node1), sizeof(node2));
    return 0;
}

Output

size1 = 12, size2 = 16
quartz
  • 747
  • 9
  • 26
  • 3
    TL;DR: The access to `int *a` better be suitably aligned, even when `node2` is used to form an array. – StoryTeller - Unslander Monica Jan 18 '18 at 10:36
  • 3
    This is because of the alignment requirement of int* which is 8, which make node2 ailgnment equals to 8, and the size of a struct must be a multiple of its alignment. – Oliv Jan 18 '18 at 10:36

0 Answers0