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
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
The Sturucture in c has following syntax and size of each sturct depends on the contents of the structure.
struct Student {
int roll;
char name[50];
float marks;
};
Here, the size of stucture is 62 bytes.
int of 4 bytes
Each char of 1 byte
flat of 8 bytes.

Login to add comment