C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Make all data pointers intercompatible

From: Thiago Macieira <thiago_at_[hidden]>
Date: Thu, 30 Jan 2025 09:36:41 -0800
On Thursday 30 January 2025 01:18:41 Pacific Standard Time Frederick Virchanza
Gotham via Std-Proposals wrote:
> class u32string {
> char32_t buf[16];
> char32_t *p;
> u32string(void) { p = buf; }
> };
>
> The pointer 'p' points to something that has an alignment requirement
> of 4. There are two consequences to this:

> (2) If I set 'p' to an unaligned address, maybe the lower 2 bits
> won't retain their value because of UB.

Correct. The standard does not allow you to use unaligned pointers, definitely
not load and store from it. Unless you're going to also submit a paper
requiring that unaligned memory accesses be allowed, I don't see the point in
requiring the pointers be required to keep those values.

When you do your memcpy() of a valid & aligned u32string to an unaligned
buffer, there's no u32string there. You did not start its lifetime and you
could not have started its lifetime because the pointer to it would be
unaligned too. It's completely irrelevant what this bag of bits contains -
it's an application/octet-stream, not application/u32string if think of MIME.

Once you copy it back to aligned area and somehow start its lifetime, the
problem disappears. So again there's nothing to be solved.

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
  Principal Engineer - Intel DCAI Platform & System Engineering

Received on 2025-01-30 17:36:46