Memory Layout of a Structure

  • by

In the previous post we discussed about structures – What structures are and how they can be useful. We also discussed how to declare and use structures. In this post, we shall look into what the memory layout of a structure looks like.

Let’s say we have the following declaration for a structure:

struct room
{
    int temperature;
    float luminosity;
    float soundLevel;
    int humidity;
};

int main()
{
    int i=0;
    struct room r[3];

    for(i=0;i<3;i++)
    {
        printf("the address of r[%d] = %d\n",i,&r[i]);
        printf("the address of r[%d].temperature = %d\n",i,&r[i].temperature);
        printf("the address of r[%d].luminosity = %d\n",i,&r[i].luminosity);
        printf("the address of r[%d].soundLevel = %d\n",i,&r[i].soundLevel);
        printf("the address of r[%d].humidity = %d\n\n",i,&r[i].humidity);
    }

What we have done here is that we have four elements for the structure room and we have created three structure variables – r[0], r[1] and r[2]

And then we run a for loop to check for the addresses of the start of each structure variable and the addresses of each element inside each structure variable.
When we execute this program, we get the following output:

the address of r[0] = 6356700
the address of r[0].temperature = 6356700
the address of r[0].luminosity = 6356704
the address of r[0].soundLevel = 6356708
the address of r[0].humidity = 6356712

the address of r[1] = 6356716
the address of r[1].temperature = 6356716
the address of r[1].luminosity = 6356720
the address of r[1].soundLevel = 6356724
the address of r[1].humidity = 6356728

the address of r[2] = 6356732
the address of r[2].temperature = 6356732
the address of r[2].luminosity = 6356736
the address of r[2].soundLevel = 6356740
the address of r[2].humidity = 6356744

If we take the first structure element r[0], we can see that the starting address of r[0] is the same as the starting address of it’s first element

the address of r[0] = 6356700
the address of r[0].temperature = 6356700

What that means is that every structure element starts at the address of its first element.
Moreover, each structure variables are placed one after another in the memory, just like in array elements.

the address of r[0].humidity = 6356712

and humidity is of type int, so, it takes 4 bytes of memory, i.e., 6356712, 6356713, 6356714 and 6356715

And

 
the address of r[1] = 6356716
the address of r[1].temperature = 6356716

This particular arrangement gives us the ability that we can pass the address of the first structure variable and incrementing the address will give us the next structure variable.

 

Memory Layout of a structure

Memory Layout of a structure

  1. Structure Elements are placed one after another in memory
  2. Structure variables are placed one after another in memory
  3. The starting address of a structure variable is same as the starting address of it’s first element

The following YouTube video will help you understand better:

In the next post, we will discuss how to pass structures and structure pointers to a function.

Sharing is Caring!!

Leave a Reply

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