Top 11 structure padding questions in C

If you are looking for structure padding questions or tricky structure padding questions, then you are at the right place. In my previous post, I created a collection of “C interview questions” that are liked by many people. So here I have tried to create some collection of structure padding questions in C that might ask by your interviewer. These structure padding questions, I have created after spending many hours. I hope these structure padding questions will be helpful.

Note: Before solving the below questions you should read this article, structure padding, and memory alignment.

In this article, I will explain a few important questions related to the structure padding in C that might be asked by your interviewer. I am using dev-c++ and X64 machine.

 

Below find the list of Best structure padding questions in C:

 

Question 1:

What is structure padding in C?

When you create the structure the compiler may insert some extra bytes between the members of the structure for alignment. These extra unused bytes are called padding bytes and this technique is called structure padding in C. Structure padding increases the performance of the processor at the penalty of memory.

If you want you can avoid the structure padding in C using the pragma pack (#pragma pack(1) ) or attribute ( __attribute__((__packed__)) ).

The below table contains the alignment of some primitive C data types,

Data Type 32-bit (bytes) 64-bit (bytes)
char 1 1
short 2 2
int 4 4
float 4 4
double 8 8
pointer 4 8

 

 

Question 2:

#include <stdio.h>

typedef struct
{

    char A;

    int B;

    char C;

} InfoData;



int main(int argc, char *argv[])
{
    //Calculate size of structure
    printf("\n Size of Structure = %d\n\n",sizeof(InfoData));

    return 0;
}

 

Output: 12

In the above declaration, Integer is the largest structure member (4 bytes) so to prevent from the penalty compiler inserts some extra padding bytes to improve the performance of the CPU. So the size of the InfoData will be 12 bytes due to the padding bytes.

 

structure padding in c

 

Question 3:

#include <stdio.h>

typedef struct
{
    int A;

    char B;

    char C;

} InfoData;



int main(int argc, char *argv[])
{
    //Calculate size of structure
    printf("\n Size of Structure = %d\n\n",sizeof(InfoData));

    return 0;
}

Output: 8

In the above example, the size of the InfoData is 8 bytes due to 2 tail padding bytes inserted by the compiler for the data alignment.

padding bytes

 

Question 4:

#include <stdio.h>

typedef struct
{
    double A; // 8-byte

    char B; // 1-byte

    char C;   // 1-byte

} InfoData;


int main(int argc, char *argv[])
{
    //Calculate size of structure
    printf("\n Size of Structure = %d\n\n",sizeof(InfoData));

    return 0;
}

Output: 16

The largest structure member is double (8 bytes), hence the compiler aligned the memory in the form of 8 bytes. So here compiler adds 6 padding bytes for the alignment, and the size of the InfoData will be 16 bytes.

structure padding and alignment

 

Question 5:

#include <stdio.h>

typedef struct
{
    int A; //size 4 byte

    int B; //size 4 byte

    char C; //size 1 byte

    char D; //size 1 byte

    float E; //size 4 byte

} InfoData;


int main(int argc, char *argv[])
{
    //Calculate size of structure
    printf("\n Size of Structure = %d\n\n",sizeof(InfoData));

    return 0;
}

 

Output: 16

The largest structure member is int (4 bytes), hence the compiler aligned the memory in the form of 4 bytes.

 

Question 6:

#include <stdio.h>

typedef struct
{
    char A; // 1-byte

    char B;   // 1-byte

} InfoData;


int main(int argc, char *argv[])
{
    //Calculate size of structure
    printf("\n Size of Structure = %d\n\n",sizeof(InfoData));

    return 0;
}

Output: 2

Largest structure member is char (1 byte), hence the compiler aligned the memory in the form of 1 byte.

Question 7:

#include <stdio.h>

typedef struct
{
    char A;
    short B;
    int C;
    char D;

} InfoData;


int main(int argc, char *argv[])
{
    //Calculate size of structure
    printf("\n Size of Structure = %d\n\n",sizeof(InfoData));

    return 0;
}

Output: 12

Largest structure member is char (4 bytes), hence the compiler aligned the memory in the form of 4 bytes.

Question 8:

#include <stdio.h>

typedef struct
{
    char A; //1 byte

    double B; //8 byte

    char C; //1 byte

} InfoData;


int main(int argc, char *argv[])
{
    //Calculate size of structure
    printf("\n Size of Structure = %d\n\n",sizeof(InfoData));

    return 0;
}

Output: 24

Largest structure member is char (8 bytes), hence compiler aligned the memory in the form of 8 bytes.

Question 9:

#include <stdio.h>

typedef struct
{
    char A; //1 byte
    char B; //1  byte
    short C; //2 byte
    int D; //4 byte

} InfoData;


int main(int argc, char *argv[])
{
    //Calculate size of structure
    printf("\n Size of Structure = %d\n\n",sizeof(InfoData));

    return 0;
}

Output: 8

Largest structure member is int (4 bytes), hence compiler aligned the memory in the form of 4bytes.

Question 10:

#include <stdio.h>

#pragma pack(push, 1)
typedef struct
{
    double A; // 8-byte
    char B; // 1-byte

} InfoData;
#pragma pack(pop)


/* main function */
int main(int argc, char *argv[])
{

    printf("\n Size of Structure = %d\n\n\n\n",sizeof(InfoData));

    return 0;
}

Output: 9

Note:  We can change the alignment of structure, union, or class using the “pack” pragma directive, but sometimes it becomes a crucial reason for the compatibility issues in your program. So it’s better always use the default packing of the compiler.

Question 11:

#include <stdio.h>

#pragma pack(push,4)
typedef struct
{
    double A; // 8-byte
    char B; // 1-byte

} InfoData;
#pragma pack(pop)


/* main function */
int main(int argc, char *argv[])
{

    printf("\n Size of Structure = %d\n\n\n\n",sizeof(InfoData));

    return 0;
}

Output: 12

 

Recommended Posts for you:

 

Reference: structure padding.

3 comments

  1. InfoData isn’t it’s just a prototype of structure?
    Should’t we need to declare a structure variable of user defined data type of InfoData if we need to allocate a memory?

    1. If you look at the example you can see InfoData is not a structure variable basically it is a new name for the structure. But if you don’t want to do you can go without new name.

Leave a Reply

Your email address will not be published. Required fields are marked *