Hash
Hash

Reputation: 229

How does c compiler allocate memory for structures?

By studying structure lead me to a question that how structures are stored in the memory by compiler which means consider

struct 
{
      int number;
      char name[nam];
 }h;

what i want to know is how the members (here it is number and name) are stored (whether they are stored in sequence or stored in random place for instance if number is stored in address 2000 and name is stored in 2990) ?

Upvotes: 4

Views: 544

Answers (6)

haccks
haccks

Reputation: 106022

Memory is allocated in a sequence for structure members:

+------------+------------------+-----+-----+-----+-----+-----+-----+
|            |     ........     |     |     |     |     |     |     |
|            |                  |     |     |     |     |     |     |
+------------+--------+---------+-----+-----+-----+-----+-----+-----+
+            +        |       name[0]                             name[nam-1]
+------+-----+        |         +------------------+----------------+
       |              |                            |
       |              |                            |
       v              v                            v
    number         padding                      name[nam]

But, unlike arrays, the allocated memory for a structure may or may not be packed, i.e, there may be some padding after the allocated space of any member (but no padding is allowed before the first member).

Upvotes: 4

Michael Burr
Michael Burr

Reputation: 340218

From C99 6.7.2.1/13 structure and union specifiers

Within a structure object, the non-bit-field members and the units in which bit-fields reside have addresses that increase in the order in which they are declared. A pointer to a structure object, suitably converted, points to its initial member (or if that member is a bit-field, then to the unit in which it resides), and vice versa. There may be unnamed padding within a structure object, but not at its beginning.

So the field name must follow the field number in your example, but there may be padding added between the two fields. There also may be padding after the name field, which is used to ensure that arrays of these structs would have each element suitably aligned.

Upvotes: 2

Maroun
Maroun

Reputation: 95968

I copied this image from my lecture in my first year. I hope it helps (It should):

enter image description here

Upvotes: 2

nesderl
nesderl

Reputation: 325

Arrays can be stored in two different ways. In your structure, if its size is constant, like char array[42] or, if they have a unknown size at compile time, be allocated using malloc and have a pointer to the array in your structure.

In the first case, one bloc of memory will contain all your structure data including you full array.

In the second case, you'll find in memory at the address of the element of your structure a pointer containing the real address of the array in the memory, so you'll only see a number (the pointer's pointed address)

Upvotes: -1

Imre Kerr
Imre Kerr

Reputation: 2428

Fields will always be in the order written, but there may be padding between them.

Upvotes: 2

Rahul Tripathi
Rahul Tripathi

Reputation: 172458

You may find the answer in Data structure alignment:

Data structure alignment is the way data is arranged and accessed in computer memory. It consists of two separate but related issues: data alignment and data structure padding. When a modern computer reads from or writes to a memory address, it will do this in word sized chunks (e.g. 4 byte chunks on a 32-bit system). Data alignment means putting the data at a memory offset equal to some multiple of the word size, which increases the system's performance due to the way the CPU handles memory. To align the data, it may be necessary to insert some meaningless bytes between the end of the last data structure and the start of the next, which is data structure padding.

Upvotes: 2

Related Questions