How to find the size of structure in C without using sizeof?

size of structure in c

In C language, sizeof() operator is used to calculate the size of structure, variables, pointers or data types, data types could be pre-defined or user-defined. Using the sizeof() operator we can calculate the size of the structure straightforward to pass it as a parameter. 

But here I am interested in calculating the size of structure in C without using the sizeof() operator. Calculating the size of structure in c without using the sizeof() operator seems to be difficult but with the help of pointers, we can do it easily. It is also an important interview question that is generally asked by the interviewer to check the understanding of pointer.

In this article, I will describe some methods to calculate the size of the structure in c without using the sizeof the operator. But it is my recommendation to use the sizeof() operator to calculate the size of the structure in a program whenever you need to calculate the size of the structure.

Here we need to use some techniques to calculate the size of the structure in C. I am describing this technique, using a few examples.

Note: Structure padding also affects the size of the structure in C.

Example  1

  • First, create the structure.
  • After creating the structure, create an array of structures, Here RamInfo[2].
  • Create a pointer to structure and assign the address of the array.
#include <stdio.h>
#include <stdlib.h>


typedef struct
{
    char Name[12];
    int Age;
    float Weight;
    int RollNumber;

} sStudentInfo;



int main(int argc, char *argv[])
{
    //create an array of structure;
    sStudentInfo RamInfo[2] = {0};

    //Create pointer to the structure
    sStudentInfo *psInfo  = NULL;

    int iSizeofStructure = 0;

    //Assign the address of array to the pointer
    psInfo = RamInfo;

    // Subtract the pointer
    iSizeofStructure = (char*)(psInfo + 1) - (char*)(psInfo);

    printf("Size of structure  =  %d\n\n",iSizeofStructure);

    return 0;
}

 

size of structure in C

Example 2

When we increment the pointer then pointer increases a block of memory (block of memory depends on pointer data type). So here we will use this technique to calculate the size of a structure.

See this link: pointer arithmetic 

  • First, create the structure.
  • Create a pointer to structure and assign the NULL pointer.
  • Increment the pointer to 1.
#include <stdio.h>

typedef struct
{
    char Name[12];
    int Age;
    float Weight;
    int RollNumber;

} sStudentInfo;


int main(int argc, char *argv[])
{
    //Create pointer to the structure
    sStudentInfo *psInfo  = NULL;

    //Increment the pointer
    psInfo++;

    printf("Size of structure  =  %u\n\n",psInfo);

    return 0;
}

 

 

If you want to learn more about the c language, here 10 Free days  C video course for you.

C tutorial

Example 3

We can also calculate the size of the structure using the pointer subtraction. In the previous article “all about the pointer“, we have read that using the pointer subtraction we can calculate the number of bytes between the two pointers.

  • First, create the structure.
  • create an array of structures, Here aiData[2].
  • Create pointers to the structure and assign the address of the first and second element of the array.
  • Subtract the pointers to get the size of the structure in c.

size of structure without using sizeof operator in c

#include <stdio.h>
#include <stdlib.h>


typedef struct
{
    char Name[12];
    int Age;
    float Weight;
    int RollNumber;

} sStudentInfo;



int main(int argc, char *argv[])
{
    //create an array of structure;
    sStudentInfo aiData[2] = {0};


    //Create two pointer to the integer
    sStudentInfo *piData1 = NULL;
    sStudentInfo *piData2 = NULL;

    int iSizeofStructure = 0;

    //Assign the address of array first element to the pointer
    piData1 = &aiData[0];

    //Assign the address of array third element to the pointer
    piData2 = &aiData[1];

    // Subtract the pointer
    iSizeofStructure = (char*)piData2 - (char *)piData1;

    printf("Size of structure  =  %d\n\n",iSizeofStructure);

}

 

size of structure without sizeof

 

Example  4

  • First, create the structure.
  • After creating the structure, create an array of structures, Here sData[2].
  • Get the address of the first element using the sData[0] and sData[1].
  • Subtract both addresses to get the size of the structure.
#include<stdio.h>

struct
{
    int a;
    int b;
} sData[2];

int main()
{
    int start, last;

    start = &sData[1].a;
    last = &sData[0].a;

    printf("\nSize of Structure : %d Bytes",start-last);
    
    return 0;
}

Output:  8 byte

 

Is sizeof for a struct equal to the sum of sizeof of each member?

No. The sizeof of a structure is not always equal to the sum of sizeof of each individual member. This is because of the extra padding byte which inserts by the compiler to avoid alignment issues. According to the C standards, alignment of structure totally depends on the implementation.

Let us see an example for better understanding:

#include <stdio.h>

typedef struct
{
    // sizeof(char) = 1
    char A;

    // sizeof(int) = 4
    int B;
    // sizeof(char) = 1
    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 the performance 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.

Recommended Posts for you



7 comments

  1. When doing pointer math I like to cast the pointers to intptr_t or uintptr_t not char*. Also sizeof is an operator and does not require parentheses unless operating on a type. E.g. sizeof (int) [required] sizeof foo_variable [not required]. By omitting the parens you tell the reader that this is an object, not a type. Lastly, %d, is for printing ints, use the macros in inttypes.h to print objects like intptr_t or %zu to print the result of the sizeof operator.

  2. Thanks mark for the suggestion. I know that sizeof is an operator, I have mentioned in the beginning of the article. I think that parentheses is good for the readability and there is no side effect for parentheses.

  3. why do we need to cast the variable to char *
    why cant we just subtarct like piData2 – piData1

Leave a Reply

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