In this chapter we shall look at below topics:
What will be the size of structure in below example?
Now why we add extra bytes?
How to avoid structure padding?
Structure Packing:
Bitfields in C++
Before we look at Structure Padding and Structure Packing, let’s see an example below:
What will be the size of structure in below example?
#include <iostream>
// for more tutorials visit www.ProDeveloperTutorial.com
using namespace std;
struct MyStruct
{
char a;
int i;
char b;
int j;
};
int main( )
{
cout<<"Size of MyStruct is "<<sizeof(MyStruct);
return 0;
}
Output:
Size of MyStruct is 16
According to our understanding, it should be 2 byte for char + 4 bytes for int + 2 byte for char + 4 bytes for int = 10 bytes.
But the output is 16 bytes. Why is difference?
This is because of structure padding.
Structure padding is a concept of adding extra bytes to make reading easier for the compiler. The extra bytes added is called as padding.
Structure Packing is not to add extra bytes.
Now why we add extra bytes?
Computer can read memory locations efficiently of 1 word size. Generally 1 word is 4 bytes in most of the computers.
So in our case, it will add 1 byte for char + 3 bytes padding + 4 bytes for int + 1 byte for char + 3 bytes padding + 4 bytes for int = 16 bytes
How to avoid structure padding?
We can avoid structure padding, by declaring higher data type on top and lower data type on the bottom as shown in the example below:
#include <iostream>
// for more tutorials visit www.ProDeveloperTutorial.com
using namespace std;
struct MyStruct
{
int i;
int j;
char a;
char b;
};
int main( )
{
cout<<"Size of MyStruct is "<<sizeof(MyStruct);
return 0;
}
Output:
Size of MyStruct is 12
Here we have added int on the top and char at the bottom. Hence the compiler will add only 2 bytes of extra padding. It will efficient for processor to read larger programs to save memory space.
Structure Packing:
We use structure packing to avoid structure padding.
We use “#pragma pack(1)” to avoid padding.
Example:
#include <iostream>
// for more tutorials visit www.ProDeveloperTutorial.com
using namespace std;
#pragma pack(1)
struct MyStruct
{
int i;
int j;
char a;
char b;
};
int main( )
{
cout<<"Size of MyStruct is "<<sizeof(MyStruct);
return 0;
}
Output:
Size of MyStruct is 10
As you can see from the above output, no padding has been added.
Bitfields in C++
Bitfields are used to reduce the memory spaces occupied by primitive data type like int, long etc.
For example, you might need for int data type to occupy only 1 bit instead of 4 bytes, then you can use bitfields.
Syntax:
<data_type> <variable_name> : <bit_size>;
int a : 2;
Note: Here the memory space is given in bits instead of bytes.
Example:
#include <iostream>
// for more tutorials visit www.ProDeveloperTutorial.com
using namespace std;
#pragma pack(1)
struct MyStruct
{
int i: 1;
int j: 2;
};
int main( )
{
cout<<"Size of MyStruct is "<<sizeof(MyStruct);
return 0;
}
Output:
Size of MyStruct is 1
As we have allocated only 3 bits in total, 1 byte is 8 bits, hence the size is displayed as 1 byte.
Example 2:
#include <iostream>
// for more tutorials visit www.ProDeveloperTutorial.com
using namespace std;
#pragma pack(1)
struct MyStruct
{
int i: 1;
int j: 10;
};
int main( )
{
cout<<"Size of MyStruct is "<<sizeof(MyStruct);
return 0;
}
output:
Size of MyStruct is 2
Now we have allocated 11 bits, and it s 8 bits + 3 bits, hence the size is 2 bytes.