Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are struct members allocated in memory?

While attempting to create a memory manager for future C programs, I've come across this question:

"when structs are allocated, are their member fields stored in the order specified?"

For instance, consider the following struct.

typedef struct {
    int field1;
    int field2;
    char field3;
} SomeType;

When allocated, will the memory addresses of the fields be in the order field1, field2, field3? Or is this not guaranteed?

like image 377
Douglas Adam Smith II Avatar asked Dec 23 '13 04:12

Douglas Adam Smith II


People also ask

How are structure members stored in memory?

Struct members are stored in the order they are declared. (This is required by the C99 standard, as mentioned here earlier.) If necessary, padding is added between struct members, to ensure that the latter one uses the correct alignment. Each primitive type T requires an alignment of sizeof(T) bytes.

Are struct members contiguous in memory?

Because the contents of a struct are stored in contiguous memory, the sizeof operator must be used to get the number of bytes needed to store a particular type of struct, just as it can be used for primitives.

Where is struct stored in memory?

Struct will be always allocated memory in Stack for all value types. Stack is a simple data structure with two operations i.e. Push and Pop . You can push on the end of the stack and pop of the end of stack by holding a pointer to the end of the Stack. All reference types will be stored in heap.

When memory is allocated to a structure?

Memory is allocated only when an instance of structure is created, till no memory is allocated. Here Structure reserved one bytes when it declare without any member declare inside it.


2 Answers

Short answer: they are allocated with the order as they declared in the struct.


Example:

#include <stdio.h>
#include <string.h>

struct student 
{
    int id1;
    int id2;
    char a;
    char b;
    float percentage;
};

int main() 
{
    int i;
    struct student record1 = {1, 2, 'A', 'B', 90.5};

    printf("size of structure in bytes : %d\n", 
        sizeof(record1));

    printf("\nAddress of id1        = %u", &record1.id1 );
    printf("\nAddress of id2        = %u", &record1.id2 );
    printf("\nAddress of a          = %u", &record1.a );
    printf("\nAddress of b          = %u", &record1.b );
    printf("\nAddress of percentage = %u",&record1.percentage);

    return 0;
}

Output:

size of structure in bytes : 16 
Address of id1 = 675376768
Address of id2 = 675376772
Address of a = 675376776
Address of b = 675376777
Address of percentage = 675376780

The pictorial representation of above structure memory allocation is given below. This diagram will help you to understand the memory allocation concept in C very easily.

enter image description here


Further reading: check out here (also the source for the above example) for C – Structure Padding and Structure dynamic memory allocation in C.

like image 173
herohuyongtao Avatar answered Oct 13 '22 01:10

herohuyongtao


You are guaranteed that field3 comes after field2, which comes after field1, and that field1 is at the start of the memory (i.e. there's no padding before field1). However, they may be padding in between the other members (and even after field3). In short, the order in which they are declared is the order in which they are laid out in memory, though exact alignment and padding is implementation defined (but there won't be any padding before the first member).

like image 24
Cornstalks Avatar answered Oct 13 '22 01:10

Cornstalks