How structures are stored in C memory

How are structures in C stored in memory?

Why is sometimes the length of the structure in memory longer than expected?

0
source share
2 answers

This is called data structure alignment .

Although this is not an ideal duplicate, you can find a good explanation of this in C on the Deposits and packaging structure .

+2
source
  • Structures are saved as a concatenation of the variables they declared to contain.
  • Variables are stored in the order in which they are declared. The start address of the structure is the start address of the first variable it contains.
  • There may be a gap between the remaining variables in the structure's memory structure (or even at the end of the structure). This is done to align variables to memory addresses from which they can be extracted.
    • Some architectures can only extract aligned data.
    • Most architectures that can receive unrelated data incur a performance penalty.
0
source

All Articles