Look at the sizeof That Thing

A structure is a multi-variable, containing several variable types, all members of the same unit. Two declarations are required for a structure: The structure itself and the variable. Programmers get into trouble with structures when determining the variable’s size, using typedef as a structure shortcut, and when declaring structure pointers.

In my books, I recommend avoiding typedef when working with structures. Sure, typedef saves you some typing (though that’s not the “type” in typedef). Until you understand structures, it’s best to avoid typedef.

The structure pointer issue is more complex. Basically, programmers working with linked lists declare a structure pointer before the structure itself is defined, which leads to all kinds of woe.

That leaves the sizeof issue for this Lesson.

The sizeof operator returns the storage size for a variable. That may seem like trivial information, but when you write data to a file, the fwrite() function requires a data-chunk size; the sizeof operator returns that value nicely. The problem with structures is on which item to use the sizeof operator: the structure itself, the structure variable, or the structure pointer?

As an example, here is the struct person, which contains a few tidbits relating to a human:

    struct person {
        char first_name[24];
        char last_name[24];
        int age;
    };

The structure contains two strings, first_name and last_name and an int value, age. My guess is that the size of this structure will be 24+24+4, or 52 bytes. That’s given that a typical int value on a modern computer is 4-bytes wide. (Though it’s bad to guess a structure size; always use sizeof instead.)

The following code declares the person structure and returns its size:

#include <stdio.h>

int main()
{
    struct person {
        char first_name[24];
        char last_name[24];
        int age;
    };

    printf("The structure 'person' has a size %lu\n",sizeof(struct person));

    return(0);
}

Here’s the output:

The structure 'person' has a size 52

I guessed correctly!

Remember that the structure itself is just a structure. Only when a variable is created are the 52 bytes of memory allocated. I can modify the code to create a person variable donald, and then use sizeof on that variable:

#include <stdio.h>

int main()
{
    struct person {
        char first_name[24];
        char last_name[24];
        int age;
    };
    struct person donald;

    printf("The structure 'person' has a size %lu\n",sizeof(struct person));
    printf("The variable 'donald' has a size %lu\n",sizeof(donald));

    return(0);
}

Here is the output:

The structure 'person' has a size 52
The variable 'donald' has a size 52

Declaring a structure variable instantly sets aside the proper quantity of memory, but that allocation doesn’t happen when you declare a structure pointer variable. This fact causes trouble for C programmers.

In the final modification to the code, I add a structure pointer variable:

#include <stdio.h>

int main()
{
    struct person {
        char first_name[24];
        char last_name[24];
        int age;
    };
    struct person donald;
    struct person *george;

    printf("The structure 'person' has a size %lu\n",sizeof(struct person));
    printf("The variable 'donald' has a size %lu\n",sizeof(donald));
    printf("The pointer variable 'george' has a size %lu\n",sizeof(george));

    return(0);
}

The output shows that a pointer variable, even though it’s a pointer to a 52-byte structure, is only the size of the pointer variable:

The structure 'person' has a size 52
The variable 'donald' has a size 52
The pointer variable 'george' has a size 8

The pointer must be initialized before it can be used. For a structure, that involves using the malloc() function to allocate the proper quantity of storage, sizeof(person), and then saving the memory location returned in the george variable. Even then, the pointer occupies only 8 bytes of storage where the structure allocated would be 52 bytes in size.

Leave a Reply