Sunday, May 9, 2021

Structure Padding in C

 

Structures in C:


Before directly jumping to structure padding we will quickly brush up on C structures basics.

Structure is a collection of multiple variables of different types. Structures are used to create user-defined data types as a collection of other data types. For example, the location co-ordination on earth consists of latitude and longitude. Let’s look at the syntax for this;


struct location
{
float latitude//Location Latitude
float longitude//Location Longitude
char name[32];  //Location Name 
char timezone[6]; //+5:30, -12:00 etc. 
};

The above syntax declares a new user-defined data type ‘struct coord’ which can be used to store the co-ordinates.

Examples of Using Structures:

#include<stdio.h>
struct name {
char fname[50];
char lname[50];
};
int main()
{
    struct name student;
    printf(“Enter your first name:”);
    gets(student.fname);
    printf(“Enter your last name:”);
    gets(student.lname);
    printf(“First Name :%s”,student.fname);
    printf(“Last Name:%s”, student.lname);
    return 0;
}

Let’s compile and run the program.

Enter the first name and the last name. The output will be as shown below:

PS D:\Projects\Hello> .\a.exe
Enter your first name:Hello
Enter your last name:World
First Name : Hello
Last Name : World 


Structure Padding in C:

Many processors expect memory for variables to be aligned based on the size of the variable. A ‘char’ of 1 byte can be allocated anywhere in memory like 0x5000 or 0x5001. And an ‘int’ of 4 bytes, must start at a 4-byte boundary like 0x5004 or 0x5008. The structure padding is automatically done by the compiler to make sure all its members are byte aligned.

The size of the below structure is 16 bytes due to structure padding:

Struct dummy {
Char ch;
Int num;
Double temp;
}

Here ‘char’ is only 1 byte but after 3 byte padding, the number starts at 4 byte boundary.  For ‘int’ and ‘double’, it takes up 4 and 8 bytes respectively.

Structures and Padding in C

The compiler used the 3 wasted bytes (marked in red) to pad the structure so that all the other members are byte aligned. Now, the size of the structure is 4 + 1 +3 = 8 bytes. The size of the entire structure is 8 bytes.  On knowing the structured padding, it is easier to redesign or rewrite the structure.

Let’s look at another example where the syntax is slightly different;

Structures and Padding in C


#include<stdio.h>
struct dummy 
{
    int num;
    double x;
    float f;
};
struct dummy1 
{
    double x;
    int num;
    float f;
};
int main()
{
    printf("size : %d  %d " , sizeof(struct dummy ), sizeof(struct dummy1));
    return 0;
}

The output will be as shown below;

size : 24  16 
[Done] exited with code=0 in 1.846 seconds


Note: As per my observation Structure memory alignment happens based on largest sized member variable. i.e. if largest member is double (8  bytes) then structure is 8 bytes aligned; if  largest member is long double (16  bytes) then structure is 16 bytes aligned.

Try running below program on your machine with different permutations and combinations to get idea of how structure memory is aligned.
Note that in str test2 Here char c[17]  have size 17 but it is not the largest sized data type



#include<stdio.h>
struct test1 {
int num;
double c;
long double x;
//float f;
};
struct test2 {
long double x;
//int num;
char c[17];
float f;
};
int main()
{
printf("size of (test1) : %d and\nsize of (test2) : %d" \
sizeof(struct test1), sizeof(struct test2));
return 0;
}

size of (test1) : 32 and
size of (test2) : 48
[Done] exited with code=0 in 2.404 seconds


No comments:

Post a Comment