Path: csiph.com!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.lang.c++ Subject: Re: Is this really necessary Date: Sat, 14 Aug 2021 02:58:13 -0700 Organization: A noiseless patient Spider Lines: 51 Message-ID: <86im08xs96.fsf@linuxsc.com> References: <8635rh1m2i.fsf@linuxsc.com> <86bl63zs5l.fsf@linuxsc.com> <86y297x7fz.fsf@linuxsc.com> <86tujuy93d.fsf@linuxsc.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader02.eternal-september.org; posting-host="e69182db6e8a2e097c43a26c68c35bd4"; logging-data="12023"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/hm3J+vTDj2lHIEwmadRpPoCbelpHgEcE=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:6p1QQitx9ZJMDEcPfRlQR5BikkI= sha1:vA6vqEHrBMY8jNnPnIw0ocNVQlg= Xref: csiph.com comp.lang.c++:80879 "Alf P. Steinbach" writes: > On 13 Aug 2021 09:16, Juha Nieminen wrote: > >> Paavo Helde wrote: >> >>> For making >2GB differences to always work reliably in 32-bit >>> program one needs to cast the pointers themselves first to an >>> (u)int64 type, then subtract. But then it is not subtraction of >>> pointers any more. >> >> This works in practice, but if we are *really* strict about the >> standard, converting a pointer to an integer is not guaranteed to >> work (without loss of information). The C standard states: >> >> "Any pointer type may be converted to an integer type. Except as >> previously specified, the result is implementation-defined. If the >> result cannot be represented in the integer type, the behavior is >> undefined. The result need not be in the range of values of any >> integer type." >> >> Notice particularly the last sentence (and, consequently, the >> second-last sentence.) >> >> So, you can do it, but it's not guaranteed to give you the correct >> result that you expect. > > If the implementation provides `uintptr_t` then you're guaranteed a > correct result for roundtrip conversion. > > C99: "any valid pointer to void can be converted to this type, then > converted back to pointer to void, and the result will compare equal > to the original pointer" > > >> I assume the C++ standard says something similar. > > The C++ standard implicitly adopts the C standard's requirements. > > In C++03 this was perhaps more clear because then the standard > stated that the C standard "is incorporated into this Standard by > reference". Note that there is some ambiguity as to which C standard is being referenced here. The Normative References list ISO/IEC 9899:1999, but section 1.1 p2 says "C++ is a general purpose programming language based on the C programming language as described in ISO/IEC 9899:1990", and section 1.2 p2 (also in Normative References) says "The library described in clause 7 of ISO/IEC 9899:1990 and clause 7 of ISO/IEC 9899/Amd.1:1995 is hereinafter called the Standard C Library."