Structure members are placed one after another in the increasing order of memory. Now a CPU with 32bit data bus width is optimized to access 32bit or 4byte at a time and thus compiler arranges structure members in such a way that it maintains 32bit memory alignment. For 64bit this alignment is 64bit or 8bytes. Structure packing and padding extra bytes in structure members comes into picture.
Packing is an option in compiler to define how the members of a structure will be aligned in memory. It is defined via pre-processor #pragma pack(
#pragma pack(1) struct pack_test_t { unsigned char m_byte; }; sizeof(struct pack_test_t) is 1 #pragma pack(4) struct pack_test_t { unsigned char m_byte; }; sizeof(struct pack_test_t) is 4 [sizeof(m_byte) + sizeof(3 padding bytes)]
About our authors: Team EQA
You have viewed 1 page out of 248. Your C learning is 0.00% complete. Login to check your learning progress.
Questions index C Questions C++ Questions Win32 MFC COM/DCOM DLL Questions
Compilers & Editors
Download Visual Studio Download XCode Download Visual Studio Code Android studio install sdk Eclipse installer Best C compilers IDEs
Development system setup
Windows media creation tool MSDN subscription Ubuntu virtualbox
New updated posts
Why learn C? Calculate weighted average