Counting Size of Struct in C

First, i didn’t guarantee that this post will give a true answer for any problem because this conclusion just made by myself based on my observation on some codes. It’s very welcome to give another opinions or answers to share more knowledge and better solution.

This post will discuss about how much a struct take a memory? because of structs is a user-defined data structures, it’s size will be relative to the content that the struct stores. For beginning let’s refresh our memory (brain) about two most-used primitive data types size.

  1. int (4 bytes / 32 bits)
  2. char (1 bytes / 8 bits)

I will use a struct named “foo” with some “bar”s as the elements. Okay let’s go to the examples:

Example 1:

struct foo
{
    int bar1;
    int bar2;
};

This struct will sized 8 bytes because it contains 2 integers (4 bytes x 2 = 8 bytes). It’s easy right?

Example 2:

struct foo
{
    int bar1;
    int bar2;
    char bar3;
};

It’s sized 12 bytes…. why? This is my first conclusion about a struct size: “Memory blocks of structs will take a size of biggest variables inside it

Example 3:

struct foo
{
    int bar1;
    int bar2;
    char bar3;
    char bar4;
    char bar5;
};

After adding two more char-type variables, the size of struct still 12 bytes…. and this is my second conclusion: “If there is still available memory in a block, other variable may use it“. The ‘bar3’ variable just used 1 bytes of 4 bytes total for a block of memory in struct, so it’s still can store 4 char-type variables without need of additional memory block. But if you’re adding 5 char-type variables it will need to add a memory block so the size of struct will be 16 bytes.

Example 4:

struct foo
{
    char bar3;
    int bar1;
    int bar2;
    char bar4;
    char bar5;
};

The code above just move the location of ‘bar3’ to first variables… Did you think the size still 12? It has changed to 16… Why?? That’s lead me to the fourth and fifth conclusion: “The variables only can share a block of memory when they are adjacent” and “If the next variable size bigger than the adjacent memory block available size, it will create a new memory block“. So the code above actually still have some free memory blocks. Explanation: block 1 just used by bar 3 because it’s only have 3 bytes free memory but bar1 need 4 bytes, so bar1 will create a new memory block. bar4 and bar5 cannot use the free memory at block 1 because they are not adjacent with bar3.

Until now i haven’t found other cases which can break these 4 conclusions, if they’re exist please share it with me…

  1. Memory blocks of structs will take a size of biggest variables inside it.
  2. If there is still available memory in a block, other variable may use it.
  3. The variables only can share a block of memory when they are adjacent.
  4. If the next variable size bigger than the adjacent memory block available size, it will create a new memory block.

Leave a Reply

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

Post Navigation