C memory structure layout with arrays

Suppose I have a C structure defined as follows:

typedef struct { double array1[2]; } struct0_T; 

How is the memory laid out? Will the string contain only a pointer or the value of two two-locale? Before I thought that the structure contains a pointer, but today I discovered (to my surprise) that the values ​​are stored there. Differences between different compilers?

+5
source share
2 answers

The structure contains two values. The memory layout is .array1[0] , then .array1[1] , optionally followed by some indentation.

Indentation is the only part of this that can vary between compilers (although in practice, when the only member of the structure is an array, there will almost certainly be no padding).

Although you may have heard that an array in C is a pointer, this is not true - an array is a cumulative type consisting of all member objects, like a structure. Just in almost all contexts of an expression, an array evaluates to a pointer to its first member.

+9
source

The specified structure declaration simply tells the compiler that variables of this type of structure struct will take sizeof(struct0_T) bytes of memory, and that memory will be allocated after an instance of a variable of this type is created.

 struct0_T s; 

Now s contains an array of two doubles . In this case there will be no indentation.

+2
source

All Articles