Can the alignof(char)
be anything but 1?
From the unofficial cppreference.com wiki:
The weakest (smallest) alignment is the alignment of the types
char
,signed char
, andunsigned char
, and it is usually 1.
The "usually" seems to imply that it could be something else.
The only thing the C standard stipulates regarding the alignment of char
is (C11 N1570 6.2.8 paragraph 1):
The alignment requirement of a complete type can be queried using an
_Alignof
expression. The typeschar
,signed char
, andunsigned char
shall have the weakest alignment requirement.
However, consider the definition of alignment (C11 N1570 6.2.8 paragraph 1, and defined similarly for C++11):
An alignment is an implementation-defined integer value representing the number of bytes between successive addresses at which a given object can be allocated.
From this, I don't think it makes sense for the alignment of char
to be anything but 1
due to the requirement that sizeof(char) ≡ 1
, which implies the distance between adjacent char
elements can only be 1
byte.
Does this make sense?
Yes. Although this statement is not explicitly specified in the standards, I suppose it can inferred from them:
N1570 6.5.3.4 The sizeof and _Alignof operators
4 When
sizeof
is applied to an operand that hastype char
,unsigned char
, orsigned char
, (or a qualified version thereof) the result is1
. When applied to an operand that has array type, the result is the total number of bytes in the array.
Taking char
for example. Say we have an char charArr[2];
. sizeof charArr
is guaranteed to be 2
, and sizeof charArr[0]
= sizeof charArr[1]
= 1
. This means two adjacent char
objects take the place of 2 bytes.
Consequently, it can be inferred that "the number of bytes between successive addresses at which a char can be allocated" is at least 1
. Also, the alignment of char
must be a positive integer, so it can't be any number other than 1
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With