Skip to content

Padding and packing #10

Answered by Ank1taS
H1manshu21 asked this question in Q&A
Apr 29, 2022 · 1 comments · 2 replies
Discussion options

You must be logged in to vote

__attribute__((__packed__)) in bmp.h [line 34 and 58] is used to avoid padded bytes among the attributes of structure.

Padding aligns structure members with default address boundaries by the processor whereas packing prevents the processor from doing padding. Padding makes the size of the struct bigger by applying some rules of memory alignment whereas packing provides the actual size of the struct to which each member is rounded off.
You can refer to this for more.

  • Rule: Every member of the struct should be at an address divisible by its size.
EXAMPLE 1
struct structure_a {
    long l;
    int i;
    char c;
};
Member Address Is address multiple of size
long 0 -7 yes (8 * 0)
int

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@H1manshu21
Comment options

H1manshu21 Apr 30, 2022
Maintainer Author

@chandrakant100
Comment options

Answer selected by H1manshu21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
documentation Improvements or additions to documentation
3 participants