2
struct abc {
    int x;
    int y;
    char a;
    char b;
};

int main() {
    struct abc test;
    printf("%d", sizeof(test));
}

The output of my code is not as I expected, I expected that the output will be 16, because as I think those variables will be stored on the memory as follows :

--------|-------|---------------|
| | | | | | | | | | | | | | | | |
--------|-------|-|-|-----------|

    x   |   y   |a|b| 6 bytes

after the first 8 bytes we will have two bytes for the two characters so the compiler need to add 6 bytes of memory, because the processor need to take 8 ytes on each cycle time.

Anyway the output is 12, so I want to know where is the problem here.

Note that: I work with 64-bit processor, in my system the size of int is 4 bytes and the size of char is 1 byte.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • 5
    There is no problem. Size `12` is perfectly fine and is guaranteeing that the structure will be aligned as it's largest member (`int`, to 4 bytes) even if put in array. – Eugene Sh. Mar 06 '23 at 15:46
  • the size 12 means the compiler add just two bytes at the end ,so after the first 8 bytes we will have just 4bytes!.but we need 8bytes for processor cycle –  Mar 06 '23 at 15:50
  • You can explore the exact layout by printing `offsetof(struct abc, x)` etc. – teapot418 Mar 06 '23 at 15:50
  • *but we need 8bytes for processor cycle* - Processor cycles are living on a different level of abstraction. – Eugene Sh. Mar 06 '23 at 15:53
  • @EugeneSh. can you explaine to me why the compiler should add just two bytes and not 6 bytes –  Mar 06 '23 at 15:54
  • 2
    I believe EugeneSh already explained that. The `int` is a 4 byte data type, so it needs to be aligned on a 4 byte boundary. The compiler adds 2 bytes of padding to make the size of the structure a multiple of 4 bytes. – Tim Randall Mar 06 '23 at 15:56
  • 1
    You might want to read this too: https://stackoverflow.com/questions/4306186/structure-padding-and-packing – Eugene Sh. Mar 06 '23 at 16:05
  • @anas "because the processor need to take 8bytes on each cycle time." --> Let us assume that is true. Why can't the size be 12 or even 10 and the processor takes those bytes and 16-12 (or 16-10) bytes from the next object when reading? – chux - Reinstate Monica Mar 06 '23 at 16:22
  • @anas If the processor reads/writes a `char`, what do you think happens to the other 7 bytes on reading/writing? – chux - Reinstate Monica Mar 06 '23 at 16:25
  • In C17 onwards, `printf("%zu\n", _Alignof(struct abc));` will print the required alignment requirement of `struct abc` in bytes. (This will be 4 because of the alignment requirement of `int` on your system.) `_Alignof(type)` can be changed to `alignof(type)` if you include `` for C17, or if you use C23.) – Ian Abbott Mar 06 '23 at 16:59
  • suppose we have only 3 members ,and the data type of each of them is char,then what is the size of that structure? –  Mar 06 '23 at 17:00
  • 1
    @anas Please add all requirements/questions/clarification to the question by [edit]ing it, don't use comments for this purpose. Mark edits of the question, especially if it might have an influence on an existing answer or comment. Alignment is implementation defined. Different compilers might use different alignment. Unaligned data might result in a program abort or in using slower or more instructions to handle this, depending on CPU, compiler, OS and settings. Adding details about your target system might allow to give more specific answers. – Bodo Mar 06 '23 at 18:29

1 Answers1

1

The structure is aligned on its largest member alignment requirement, which is int with an alignment value of 4. Hence only 2 padding bytes are needed after members a and b to achieve proper alignment, for a total size of 12 bytes, multiple of 4.

While it is possible for the compiler to add extra padding to align this structure on 16 byte boundaries, it is unlikely because such choices must be made consistent across all programs for the target architecture. In practice, the compiler only inserts the necessary padding to align all structure elements on their required alignment, including padding at the end of the structure to ensure the next element in an array of structures is correctly aligned.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • suppose we have only 3 members ,and the data type of each of them is char,then what is the size of that structure? –  Mar 06 '23 at 16:59
  • @anas Probably 3, but the compiler is allowed to add extra padding if it wants. – Ian Abbott Mar 06 '23 at 17:00
  • @lanAbbott yeah the size is 3,which make me confused.so i want to learn more about how padding and alignment works ,do you know a good course for that subject –  Mar 06 '23 at 17:04
  • 1
    @anas The key things to remember are that the size of a type is a multiple of its required alignment, and the required alignment of a struct or union type is *at least* (or in most implementations, the same as) that of the member with the strictest alignment. The first member will be aligned to the overall alignment of the struct type, but padding may be needed between some members to align them, including padding after the last member to align the next element of an array of the struct type. The compiler is free to add extra padding, but most implementations use the minimum required padding. – Ian Abbott Mar 06 '23 at 17:37