c++ - Alignment of fundamental type and structure with only this fundamental type inside -


there code:

#include <iostream>  struct {    double a; };  int main(){    std::cout << alignof(a) << std::endl; // prints 4    std::cout << alignof(double) << std::endl; // prints 8    return 0; } 

why alignment of structure a , raw double type different? i'm using linux 32 bit.

i found possible explanation here: implementation of alignof

amazing facts alignment

on modern x86 processors, type double efficiently aligned @ multiples of 8. , in fact, gcc aligns "free" doubles @ multiples of 8 within stack frames. unfortunately, ancient abis require alignment of double within struct 4. unexpected consequence that, on x86 linux, gcc has

  struct double { double d; };   __alignof__ (double) == 8   __alignof__ (double) == 4; 

the same ancient abis specify size of long double 12. because alignment must factor of size, have curious situation that:

  __alignof__ (double) == 8   __alignof__ (long double) == 4; 

even though long double "wants" @ least aligned double.

fortunately, doesn't matter much, because never care alignment of types aren't struct members.


Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -