Structure alignment in C ++

The volume is in x86.
For example, we have this structure:

struct X{
   int    a;   
   char   b; 
               // compiler will pad 3 bytes here
   double c;
};
X x;

I think alignment xshould be the largest member in it, in this case it is double , which is 8 bytes,
so it &xshould be a multiple of 8 , am I right?

However, after some tests, my compiler (msvc 2013) says it &xcan also be a multiple of 4 , but not 8 .

Does this not mean that &x.cit will also be a multiple of 4 ?

Where do I misunderstand?

+4
source share
3

C ++ [, , ] , . (, , ) . , ...;)

FPU [ SSE ] x86 "double" , , 3 b c 4 . .

- ( ), struct 8 .

+3
+1

, , , struct, "" ++ .

:

  • int, char double , , , , .
  • , , , , , , struct .

As a result, for this kind of operation you should just stick to the documentation for your compiler of your choice and to dive into the features that it offers you, for example, here you can find an overview of how to package data in gccvsmsvc .

+1
source

Source: https://habr.com/ru/post/1532540/


All Articles