C++ Logo

std-discussion

Advanced search

Re: Alignment and addresses

From: Lénárd Szolnoki <cpp_at_[hidden]>
Date: Thu, 06 Jul 2023 07:36:24 +0100
Hi,

On 6 July 2023 00:46:50 BST, Brian Bi via Std-Discussion <std-discussion_at_[hidden]> wrote:
>On Wed, Jul 5, 2023 at 6:54 PM Bernhard Manfred Gruber via Std-Discussion <
>std-discussion_at_[hidden]> wrote:
>
>> Hi,
>>
>> I am trying to figure out where the standard, specifically the object
>> model, defines alignment and its requirements on addresses.
>>
>> In [basic.align]#1 it says:
>> Object types have alignment requirements ([basic.fundamental],
>> [basic.compound]) which place restrictions on the addresses at which an
>> object of that type may be allocated. An alignment is an
>> implementation-defined integer value representing the number of bytes
>> between successive addresses at which a given object can be allocated.
>>
>> I am surprised by the second sentence, that the alignment of a type is the
>> difference between two addresses at which objects of this type can be
>> placed. It says nothing on the value of the addresses themselves. For that
>> matter, values of type double could be allocated at the addresses 0x1, 0x9,
>> 0x11, 0x19, etc.
>>
>> Digging deeper, I started wondering if the definition of alignment is
>> vague out of necessity, because it seems the standard also does not define
>> whether an address is an integer. It is described in various places that an
>> address (represented by a pointer) can be value-preservingly converted to
>> an integer and back, but that does not describe the nature of an address.
>>
>
>Correct.
>
>
>>
>> In any way, I would be glad if someone could point me to the right place
>> that establishes that alignment, which is defined as an integer, requires a
>> divisibility of an address by that number.
>>
>
>For the reasons above, such a requirement would not be well-defined, and
>therefore, does not exist in the standard.
>

The standard does talk about alignment of single pointers, but I don't think it defines it properly. std::align and std::assume_aligned also comes to mind in addition to allocation functions.

I also think that this is a hole in the standard. I think it's possible to define it in an abstract manner without exposing the underlying integer nature of pointers/addresses in most implementations.

Something along these lines:
* "aligned" is a relationship between an address and a number that is a power of two
* pointer values represent addresses
* if an address is aligned 2^N, then it's aligned 2^M for every 0<=M<=N.
* every address is aligned 1
* details how pointer arithmetic preserves alignment (underlying idea: if an address is aligned 2^N then moving the address by multiples of 2^N bytes, then the resulting address is also aligned 2^N)
* the address of objects with alignment requirement x has to be aligned x.

Cheers,
Lénárd

>
>>
>> Thanks a lot!
>> Bernhard
>>
>> --
>> Std-Discussion mailing list
>> Std-Discussion_at_[hidden]
>> https://lists.isocpp.org/mailman/listinfo.cgi/std-discussion
>>
>
>

Received on 2023-07-06 06:36:31