|
|
Subscribe / Log in / New account

Possible solution

Possible solution

Posted Oct 25, 2024 4:26 UTC (Fri) by Baughn (subscriber, #124425)
In reply to: Possible solution by a3f
Parent article: realloc() and the oversize importance of zero-size objects

Wait, are you allowed to compare pointers from different allocations? I thought that was UB.


to post comments

Possible solution

Posted Oct 25, 2024 6:01 UTC (Fri) by NYKevin (subscriber, #129325) [Link] (1 responses)

You may compare arbitrary pointers *for equality.* You only invoke UB if you compare them for ordering.

Note however that this assumes the pointers were legitimately obtained in the first place. Pointer types may have trap representations, so you cannot just cast some arbitrary pile of bytes into a pointer and expect everything to work correctly (even if you never dereference it).

Possible solution

Posted Oct 28, 2024 2:45 UTC (Mon) by ianmcc (subscriber, #88379) [Link]

In C++, p < q is undefined behavior for unrelated pointers p,q. But std::less<T*>(p,q) is well-defined, and must give a strict total ordering. Gcc had a bug on this, since fixed: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78420


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds