Are POD types always aligned?

For example, if I declare a long variable, can I assume that it will always be aligned on the border "sizeof (long)"? Microsoft Visual C ++ online help says so, but is that standard behavior?

Additional Information:

and. You can explicitly create an inconsistent integer (* bar):

char foo [5]

int * bar = (int *) (& foo [1]);

b. Apparently, #pragma pack () only affects structures, classes, and unions.

with. The MSVC documentation says that POD types are aligned in their respective sizes (but always or by default, and this is standard behavior, I don't know)

+3
source share
6 answers

, , , . , VC ARM-, x86.

Microsoft VC , , , #pragma /Zp. , , , POD 8 . 8- .

, 1 .

#pragma pack(push)
#pragma pack(1)    
struct Example
{
   short data1;     // offset 0
   short padding1;  // offset 2
   long data2;      // offset 4
};
#pragma pack(pop)

padding1 2.

:

, . x86 . . , Alpha , . , , . . __unaligned VC , x86 (, CE).

+9

. pack() #pragma.

, ++ Standard - .

+3

C ++ - . x86 , , . , , .

+1

, .

?

, sizeof() .

, :

#define ALIGNMENT_OF( t ) offsetof( struct { char x; t test; }, test )
0

Depends on the compiler, pragmas and optimization level. With modern compilers, you can also choose to optimize time or space, which can also change type alignment.

0
source

Typically, this is because reading / writing on it is faster. But almost every compiler has a switch to disable it. In gcc, its -malign is ???. With aggregates, they are usually aligned and calibrated based on the alignment requirements for each element within.

0
source

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


All Articles