C++ Logo


Advanced search

Re: [std-proposals] [[packed]] std::unaligned

From: Thiago Macieira <thiago_at_[hidden]>
Date: Wed, 20 Dec 2023 09:35:16 -0300
On Tuesday, 19 December 2023 14:07:40 -03 Frederick Virchanza Gotham via Std-
Proposals wrote:
> Just as an aside. . . if an object contains a pointer to itself . . .
> then relocating it from unaligned space into aligned space could get a
> little more complicated if the pointer inside it is anything other
> than 'void*' or 'char*', because a pointer such as 'long double*'
> might have less bits of precision for the memory address than a
> 'char*'

Another very good reason why one shouldn't apply std::unaligned to arbitrary
types. The types that can exist unaligned are few and well-known. I have yet
to be convinced that anything non-trivial should ever be unaligned; you've
just shown that some trivial ones may not be able to be so either.

This also supports Arthur's assertion that an arbitrary type can't physically
exist at an arbitrarily-aligned address.

> (By the way has there ever been such an implementation of
> C++?)

Not that I know of and answering for C. Certainly none that are extant right
now for C++.

This is in theory possible, but in practice it's not useful because the C
standard requires that char be able to address any byte in memory that any
other pointer can (that's actually the definition of byte). That would imply
that a char* would need to be bigger than other pointers, which is not allowed
either, or that the other types waste bits.

I could conceive of a machine that automatically shifts the pointer addresses
by the size of the datum being loaded or stored -- that is, the "pointers" are
actually word counts from address 0 for the type in question. As I said, I've
never heard of such machine, none such has been developed since the 1980s at
least, and no one would develop such now because there's just too much code
out there that assumes pointers are, indeed, interconvertible.

Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
   Software Architect - Intel DCAI Cloud Engineering

Received on 2023-12-20 12:35:19