Does the alignment type change the font size?

I wrote the following code and tested it on gcc:

// firstly some platform-specific workarounds
#if _MSC_VER
#define ALN_BEGIN(x) __declspec(align(x)) 
#define ALN_END(x)
#define alignof(x) __alignof(x)
#else
#define ALN_BEGIN(x)
#define ALN_END(x) __attribute__((aligned(x)))
#define alignof(x) __alignof__(x)
#endif

// struct have three 4-byte members, but aligned at 32 byte
ALN_BEGIN(32) struct Foo
{
    float a;
    float b;
    float c;
} ALN_END(32);

// show its size and alignment
int main(int argc, char** argv)
{
    printf("size %d, alignment%d\n", sizeof(Foo), alignof(Foo));
}

When I compile it with gcc and run, although Foo has only 12 bytes for all its members, I sizeof(Foo)got 32, the alignment size. Is the size expandable by the language standard (which is reliable) or is it just a feature for GCC?

I am creating an object pool class, so I need to work exactly with size and type alignment.

+4
source share
2 answers

, . sizeof (X) X . - , sizeof .

++ 14, 5.3.3/2:

... , , , .... , . , n n .

( )

+9

sizeof ( char , 1 ), . ,

  • sizeof(float) - 4;
  • floats;
  • 32 .

, .

+1

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


All Articles