saving . . . saved Size of structure has been deleted. Size of structure has been hidden .
Size of structure
Title
Question
I want to know how to reduce the size of structure which is showing 20 byte in this case to 18 byte?
struct A
{
int a;
char c[6];
double d;
};
sizeof structure = 20 byte
I am using Ubuntu 12.04 LTS
gcc 4.6.3 compiler

Advance-C General None min None sec 09-02-15, 12:19 a.m. ajit_mishra85

Answers:

there is something called structure padding or data alignment

Computers read and write to memory in small memory blocks called word sized chunks.
(for a 32bit system the word size is 4byte)

So for CPU's convenience to read and write data in word sized chunks (memory blocks)it arranges the data in multiples of 4byte. This is the reason for structure padding.
in this case

struct A
{
int a; // 4 bytes
char c[6]; // 6*1 = 6bytes <<-- here it is padded with 2 blank memory blocks to attain 8 bytes(multiple of 4)
double d; // 8bytes
}; // total =18






09-02-15, 12:58 p.m. sgk


Log-in to answer to this question.