Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do class/struct members always get created in memory in the order they were declared?

This is a question that was sparked by Rob Walker's answer here.

Suppose I declare a class/struct like so:

struct {      char A;     int B;     char C;     int D; }; 

Is it safe to assume that these members will be declared in exactly that order in memory, or is this a compiler dependent thing? I'm asking because I had always assumed that the compiler can do whatever it wants with them.

This leads into my next question. If the above example causes memory alignment issues, why can the compiler not just turn that into something like this implicitly:

struct {      char A;     char C;     int B;     int D; }; 

(I'm primarily asking about C++, but I'd be interested to hear the C answer as well)

Related topics

  • Why doesn't GCC optimize structs?
like image 561
Jason Baker Avatar asked Nov 11 '08 14:11

Jason Baker


People also ask

Does the order of members in a struct matter?

The order of fields in a struct does matter - the compiler is not allowed to reorder fields, so the size of the struct may change as the result of adding some padding.

Are struct members contiguous in memory?

They will not necessarily be contiguous in memory. This is due to struct padding. then they most likely will not be. However, in your particular case, you will still likely get padding after gender , to realign the struct to 8 bytes.

What happens in memory when a structure is declared?

Does structure declaration occupies memory? No - you won't consume memory until you declare a variable on the stack, heap, or shared memory. You're simply defining a new data type that consists of more than one other data type.

Are structs 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.


1 Answers

C99 §6.7.2.1 clause 13 states:

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.

and goes on to say a bit more about padding and addresses. The C89 equivalent section is §6.5.2.1.

C++ is a bit more complicated. In the 1998 and 2003 standards, there is §9.2 clause 12 (clause 15 in C++11):

Nonstatic data members of a (non-union) class declared without an intervening access-specifier are allocated so that later members have higher addresses within a class object. The order of allocation of nonstatic data members separated by an access-specifier is unspecified (11.1). Implementation alignment requirements might cause two adjacent members not to be allocated immediately after each other; so might requirements for space for managing virtual functions (10.3) and virtual base classes (10.1).

like image 196
aib Avatar answered Oct 09 '22 09:10

aib