For any object type T
is it always the case that sizeof(T)
is at least as large as alignof(T)
?
Intuitively it seems so, since even when you adjust the alignment of objects like:
struct small {
char c;
};
above what it would normally be, their "size" is also adjusted upwards so that the relationship between objects in an array makes sense while maintaining alignment (at least in my testing). For example:
struct alignas(16) small16 {
char c;
};
Has both a size and alignment of 16.
char x[12]
orint y[200]
or astruct { int a; char b; float c; }
? – Wiredrawsizeof
(12 and I*200, respectively) is larger than alignof (1 and I respectively), where I is sizeof(I). – Arbogastsizeof(T) == alignof(T)
for any given combination of T and architecture. The alignment value might be larger or smaller, it's impossible to say with any certainty. If you have a smaller list of types and one architecture you can run tests to find out. – Wiredrawfloat
is 4 bytes,sizeof( float )
returns 4, but the system architecture requires that afloat
be on an 8-byte boundary", where does that lead? Offhand, I think that means an array offloat
would be broken. – Xyloidsizeof(T) == alignof(T)
in general. It is trivial to show that for examplestruct S { char a,b; };
usually has size 2 and alignof 1. My question is about>=
not==
though... – Arbogastsizeof(long double)==8
whilealignof(long double)==16
depending on your compiler and OS combination. Likewise,sizeof(long long)==8
whilealignof(long long)==4
. – Wiredrawlong double
since in particular something likemalloc(N * sizeof(long double))
would allocate insufficient memory, and array indexing withsizeof()
would also be broken. – Arbogastsizeof(long double)==8
Not on my Centos 6 machine.sizeof( long double == alignof( long double ) == 16
. – Xyloidgcc
orclang
? To do a deeper investigation here a test script would be useful. – Wiredrawgcc version 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC)
– Xyloidsizeof(T)
to matchalignof(T)
? – Roccorochsizeof(T[10]) == 10 * sizeof(T)
? That would seem to preclude padding. If it's not guaranteed, then we have a problem since almost any non-trivial C++ program would seem to assume this somewhere. – Arbogastsizeof(T)
, but no compiler does that and all the extra padding has to go at the end or subscripting won’t work. That still means arrays of types with alignment greater than size would be problematic. – Roccorochsizeof(a)/sizeof(a[0])
to calculate array sizes, as well as any use ofmalloc()
oroperator new
to allocate arrays. It probably deserves a separate question though. – Arbogastoperator new[]
can allocate more memory than that, to allow for some bookkeeping. Since all the unread padding bytes would be at the end,malloc
ing something smaller would be fine. I agree that the first would break, which is probably part of why nobody has built a compiler that way (the rest of the reason being that there would be little if any point). – Roccorochsizeof(T[10])
can ever be unequal to10 * sizeof(T)
. That's the programmer visible size of an array and it's the crux of the argument in the top answer below. – Arbogast