Why Structure Padding required [ SOLVED ]

Thread Starter

Kittu20

Joined Oct 12, 2022
511
Why does the compiler allocate extra space when we use a structure in a code where one member is of integer type and the other is of character type? I don't understand the necessity for structure padding in this scenario. Specifically, I observed that the compiler allocates a total of 8 bytes of memory (4 bytes for the int type object and 4 bytes for the char type object). on window using GCC compiler

I found a link https://en.m.wikipedia.org/wiki/Data_structure_alignment that provides a description, but I don't understand data structure alignment
 

Papabravo

Joined Feb 24, 2006
22,077
If it allocates the padding in a consistent way, the compiler will know that the next available address in the block will be aligned to a proper boundary. There is generally very little penalty for wasting space in memory anymore. It was a problem 50 years ago when memories were teensy tiny, but not any more.

My favorite short story is "The Elephant's Child" by Rudyard Kipling.
 

Thread Starter

Kittu20

Joined Oct 12, 2022
511
the compiler will know that the next available address in the block will be aligned to a proper boundary.
I am trying to understand description given link I provided.

Can you please help me to understand what's meaning of " aligned " in this context.?
 

BobTPH

Joined Jun 5, 2013
11,496
A four byte integer must be at an address that is 0 mod 4 for best performance, and even for correctness in some architectures. That is what we mean by alignment, we align the integer at its natural boundary.
 

WBahn

Joined Mar 31, 2012
32,783
Why does the compiler allocate extra space when we use a structure in a code where one member is of integer type and the other is of character type? I don't understand the necessity for structure padding in this scenario. Specifically, I observed that the compiler allocates a total of 8 bytes of memory (4 bytes for the int type object and 4 bytes for the char type object). on window using GCC compiler

I found a link https://en.m.wikipedia.org/wiki/Data_structure_alignment that provides a description, but I don't understand data structure alignment
Modern computers process information in larger chunks that single bytes. If it more naturally works on 4-byte chunks, it is worth wasting some memory in order to make both the individual members of the structure take up a multiple of four bytes each and for the structure as a whole to take up a multiple of four bytes.

Think of it like this -- you have a series of boxes that can each hold four smaller containers. You now have three containers of blue power and three containers of red powder. You could put three blues and one red into one box and the remaining two reds into the second box, but can you see some utility is putting the three blues in one box and the three reds in another box, even though you are "wasting" some space? If you want to access the red powder, you only have to access one box instead of two.
 
Top