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: Thu, 10 Jun 2021 11:29:45 -0700 Organization: A noiseless patient Spider Lines: 51 Message-ID: <86lf7hd146.fsf@linuxsc.com> References: <86tum9d4vs.fsf@linuxsc.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader02.eternal-september.org; posting-host="1275637d9bb82fd4d4c8fe0cf62030c2"; logging-data="30441"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/HjZW6svCA6xSNgvHTsS+rbOyKPz7V8X8=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:EQHWHVXON4efpWy2FlmQ/Nkay0s= sha1:NPUY69MAHo/3M5PHel/JJNqpIxY= Xref: csiph.com comp.lang.c++:80257 Paavo Helde writes: > 07.06.2021 19:19 Tim Rentsch kirjutas: > >> Paavo Helde writes: >> >>> 05.06.2021 16:06 Bonita Montero kirjutas: >>> >>>>> No. Subtraction of pointers is defined as the difference in their >>>>> indexes within a single array. >>>> >>>> That coudn't be true because you can cast any pointer-pair >>>> to char *, subtract them and use the difference for memcpy(). >>> >>> This holds only for linear memory model. While this is a dominant >>> memory model nowadays, the C++ language is old enough to take also >>> other memory models (like segmented ones) into account. In segmented >>> memory models, pointer arithmetic only works in a single segment, and >>> accordingly the arrays are limited to a single segment. There is no >>> such limitation for struct members. >> >> In C there is, because any object can be treated as an array >> of character type, and that includes structs. >> >> In C++ the rules are so complicated that no one knows whether >> that reasoning applies, so the safest course of action is not >> to use C++ on machines that use segmentation. > > I'm pretty sure that any C++ implementations on segmented > architectures would require also structs and classes to fit in a > single segment, it's much easier that way. > > Anyway, this is not really important because Bonita talked about "any > pair of pointers", no mention of structs was made. > > When you take one pointer from say DS segment and the other from ES > segment, then calculating their difference might not have any meaning > for the program, not to speak about fitting this difference into a > size_t variable or copying this memory range somewhere. At best you > could copy a tail of the DS segment and the head of the ES segment > somewhere, but why not vice versa? Yes, as a practical matter no actual C++ implementation is going to accommodate objects that cross a segment boundary. My question though was about what the C++ standard allows for pointers into a single struct object (of the contiguous variety). I know that C does allow pointer arithmetic for such pointers (ie, character pointers), but my efforts to discover whether the C++ standard allows it have not been successful.