13

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, and unsigned 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 types char, signed char, and unsigned charshall 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?

Rufflewind
  • 8,545
  • 2
  • 35
  • 55
  • 3
    http://stackoverflow.com/a/4638295/1774667 -- the array argument is persussive. Arrays are packed by `size`, and elements must be multiples of `alignof`, thus `alignof(T)` divides `sizeof(T)`. As `sizeof(char)` is 1, and `alignof(char)` > 0, `alignof(char)` is 1. Other answers in that question cover the various standard quotes; I might even flag this as a dupe of that question (as its answer implies this one). – Yakk - Adam Nevraumont Apr 23 '16 at 01:25
  • Please provide a reference to the specification or standard of that language C/C++. The standards you cite are for C. – too honest for this site Apr 23 '16 at 01:26
  • @Yakk: Sorry, but what means "persussive"? – too honest for this site Apr 23 '16 at 01:27
  • If it's implementation defined doesn't that mean it's not guaranteed? – ChiefTwoPencils Apr 23 '16 at 01:31
  • 2
    @olaf persuasive typed on a phone keyboard without spellcheck enabled. – Yakk - Adam Nevraumont Apr 23 '16 at 01:34
  • I think the problem is guaranteeing what adjacent means. For example, defining two global chars one right after the other one does not guarantee that they actually exist in memory next to each other. Also, defining an array of structures containing 3 chars each does guarantee the compiler does not add padding to each structure. – Pemdas Apr 23 '16 at 01:45
  • @Olaf: I had a feeling someone would be unhappy that I wrote it that way. Hopefully the new title is more tolerable? – Rufflewind Apr 23 '16 at 03:15
  • @Yakk: Thanks for the link! That is indeed a good argument. – Rufflewind Apr 23 '16 at 03:17
  • @Pemdas: arrays are “contiguous” according to the standard, so doesn't that imply they are adjacent? – Rufflewind Apr 23 '16 at 03:23
  • someone should submit a DR I guess - it's clear from dozens of possible arguments that it must be `1` – M.M Apr 23 '16 at 04:04

2 Answers2

7

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 has type char, unsigned char, or signed char, (or a qualified version thereof) the result is 1. 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.

Evg
  • 25,259
  • 5
  • 41
  • 83
nalzok
  • 14,965
  • 21
  • 72
  • 139
0

I'm not a language lawyer, but if we look at section 6.5.3.4 paragraph 3 of C:

When applied to an operand that has type char, unsigned char, or signed char, (or a qualified version thereof) the result is 1.

To me, this doesn't seem to be a requirement that the size of char must be one, just the result of the operand sizeof must return 1. Alignment also is not the same thing as size. Alignment is dictated by the ABI, not the C or C++ standards. Of course, it is true that the alignment of char is 1 on x86, x86_64 and so on but it's not a hard requirement.

bird jesus
  • 19
  • 1