Search

Sponsored Links

Meta

Categories

Archives

Recent Posts

RSS Feeds

03
Nov

C Structure Padding

Related Blog Items

  • to access a structure member fastly, structure need to be padded
  • Padding will be done for each and every member, if required, not for the entire structure
  • Alignment : data storing boundary address starts at aligned address

Examples:

Size of int = 4 bytes

size of char = 1byte

alignment 64 bit (4 bytes)

Struct

{

int a ;

char b ;

} ;

size of struct = 8

Struct

{

int a ;

char b ;

int c ;

short int d ;

char e ;

} ;

size of struct = 16

+—————————-+
| a | b(3) | c | d e (1)|
+—————————-+
() padded bytes

Is there any way to supress this padding ?

Yes, using pragmas , typically pragma look like this

#pragma pack(1)

Why padding ?

For faster access to the members.

Popularity: 14%

You need to log on to convert this article into PDF


Related Blog Items

2 Comments

Leave a comment

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-spam image