c++ - Does type alignment modify type size? -
i wrote following code , tested on gcc:
// firstly 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 3 4-byte members, aligned @ 32 byte aln_begin(32) struct foo { float a; float b; float c; } aln_end(32); // show size , alignment int main(int argc, char** argv) { printf("size %d, alignment%d\n", sizeof(foo), alignof(foo)); }
when compile using gcc , run, although foo has 12 bytes members, sizeof(foo)
got 32 alignment size. size expansion according language standard (which reliable) or feature gcc?
i'm making object pool class, have precisely work type size , alignment.
yes, consistent standard. sizeof (x)
defined offset in bytes between starting addresses of 2 consecutive x
objects in array. if such objects cannot follow each other tightly due alignment restrictions, sizeof
increases accordingly.
quoting c++14, 5.3.3/2:
... when applied class, result number of bytes in object of class including padding required placing objects of type in array. ... when applied array, result total number of bytes in array. implies size of array of n elements n times size of element.
(emphasis mine)
Comments
Post a Comment