Most compilers automatically align data values with the platform word size or data size, whichever is smaller. The vast majority of consumer and corporate processors use 32-bit word size. (Even 64-bit systems usually use 32 bits as their native word size)
Thus, ordering the members in your structure can lead to some memory. In your particular case, you are fine. I will add in the comments the actual trace of the used memory:
typedef struct structc_tag{ char c;
This rule also applies to structures, so even if you rearrange them so that the smallest field is the last, you will still have a structure of the same size (16 bytes).
typedef struct structc_tag{ double d;
If you were to declare more fields less than 4 bytes in size, you can see some memory reductions if you grouped them by size. For example:
typedef struct structc_tag{ double d1; // 8 bytes double d2; // 8 bytes double d3; // 8 bytes int s1; // 4 bytes int s2; // 4 bytes int s3; // 4 bytes short s4; // 2 bytes short s5; // 2 bytes short s6; // 2 bytes char c1; // 1 byte char c2; // 1 byte char c3; // 1 byte // 3 bytes (padding) } structc_t; // total: 48 bytes
Declaring a stupid structure can waste a lot of memory if the compiler does not reorder your elements (which, in general, will not, without an explicit explanation)
typedef struct structc_tag{ int s1; // 4 bytes char c1; // 1 byte // 3 bytes (padding) int s2; // 4 bytes char c2; // 1 byte // 3 bytes (padding) int s3; // 4 bytes char c3; // 1 byte // 3 bytes (padding) } structc_t; // total: 24 bytes // (9 bytes wasted, or 38%) // (optimal size: 16 bytes (1 byte wasted))
Parties double more than 32 bits and, accordingly, according to the rule in the first section, 32 bits are aligned. Someone mentioned a compiler option that changes alignment, and that the default compiler option is different between 32 and 64-bit systems, this is also true. So the real answer about doubles is that it depends on the platform and the compiler.
Memory performance is determined by the words: loading from memory occurs in stages, which depends on the placement of data. If the data covers one word (i.e. word alignment), only that word needs to be loaded. If it is not aligned correctly (i.e., Int is at 0x2), the processor must load 2 words in order to correctly read its value. The same applies to doubles, which usually take up to 2 words, but if they are offset, take 3. On 64-bit systems, where 64-bit quantities can be loaded, they behave like 32-bit ints on 32-bit systems if they are correctly aligned, they can be extracted with one load, but otherwise they will require 2.