C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Standardising 0xdeadbeef for pointers

From: Thiago Macieira <thiago_at_[hidden]>
Date: Thu, 31 Jul 2025 08:41:12 -0700
On Thursday, 31 July 2025 08:23:08 Pacific Daylight Time zxuiji wrote:
> It's useful to know a standard range to apply. For example libraries like
> boost could detect if pointers passed to them are in said range and catch
> the problem before a segfault can happen. They can in turn exit the thread
> but not the whole app/game/etc.

I disagree on THAT motivation. An invalid pointer is an invalid pointer and
passing it violates the generic preconditions of any function. Do it at your
own peril, because the callee has no obligation to verify with the OS that it
is valid. And since you mentioned threading, any answer of whether it is valid
or not is also racy.

What I can see a reason for is to use the pointer as a union for other
content. We use it in QMutex, for example, to indicate an uncontended mutex.
We are sure that any address ±alignof(max_align_t) of null is invalid, as are
misaligned pointers for the object type in question.

We *could* use more bits (10 instead of 3) for other uses, but quite frankly
haven't needed.

So as usual: start with a good motivation for this.

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

Received on 2025-07-31 15:41:15