Structure - Memory Allocation - Structure Padding

Structure - Memory Allocation - Structure Padding

Solution Manual

54 года назад

74 Просмотров

// STRUCTURES AND UNIONS

// TOPIC ==== STRUCTURE PADDING

#include stdio.h
int main()
{
struct test
{
int a; // 4 bytes allocated for a variable
int b; // 4 bytes allocated for b variable
char c[2]; // it will also give the same result but empty space is reduced from 3 to 2 // 1 byte allocated for c variable:::::: By definition Total Size is : 4+4+1=9
}t;

printf("%d",sizeof(t)); // but we got 12 as output this is due to padding space .
return 0;
}

// OUTPUT IS 12

// 4 bytes for a
// 4 bytes for b
// 1 byte for c
// 3 bytes for empty space

// total = 4+4+1+3=====12

Тэги:

#memory_allocation #structures #padding #structure_padding #structure_memory #structure_size
Ссылки и html тэги не поддерживаются


Комментарии: