c++ - memory alignment- total size of structure multiple of structure alignement and not processing size -
In the previous post, I understand why we should take alignment for a structure similar to the largest feature size.
Now, I would like to know that once we have chosen this alignment, we have to do padding so that the size of the total structure is of a majority of alignment structure and not the size of the processor word.
Here's an example:
#include & lt; Stdio.h & gt; // Alignment requirements // (Typical 32 bit machine) // four 1 byte // less int 2 bytes // init 4 bytes // double 8 bytes typedef struct structc_tag {char c; Double D; Ints; } Structc_t; Int main () {printf ("sizeof (structd_t) =% d \ n", size (structd_t)); Return 0; } We can think that the size of structd_t is equal to 20 bytes:
char c; Four padding 1 [7] double D; Ints; Because we've got a structure alignment equal to 8 (double D).
But, in fact, the total size is equal to 24 because 20 is not a value of 8 and we have to do padding after "int s" (four padding 2 [4]).
If I take an array of structure, then the first element of each structure is a good ad for the 32 bit processor (0, 20, 40) because the size of the processing word 20, 40 ... 4 Are there.
So why did I have to paddle for a multi of 8 (structure alignment), in this example 24 bits, instead of having 20 bytes (which is good for 32bit processor (word size = 4 bytes) Advertise: 0, 20, 40 ...?
Thanks for your help
Consider the struct_t array [n] . If the size of the structure is 20 (not many of the eight), the second element was aligned to the 4-byte border. < P> To clarify: array [0] Address to 0 . If the size of the structure is e 20 , the array [1] starts with the address 20 , And this is the d address at land 28 , which is not a proper alignment for a double.
Comments
Post a Comment