Is D's pointer subtraction more permissive than C (and C++)?

Patrick Schluter Patrick.Schluter at bbox.fr
Sun Apr 3 12:59:05 UTC 2022


On Friday, 1 April 2022 at 20:22:46 UTC, Ali Çehreli wrote:
> On 4/1/22 10:39, Steven Schveighoffer wrote:
>
> > I don't see how the compiler/optimizer
> > can make some other decision based on the subtraction not
> being between
> > two pointers to the same block of memory.
>
> I think this rule is related to C's accepting wildly different 
> platforms, some of which may have different kinds of memory. 
> Two pointers to different kinds of memory may not be subtracted.
>
> Ali

In C you're not even allowed to cast a function pointer to a data 
pointer. Posix requires that they are (or else no dynamic 
linking).

These C restictions are put in place because of the all the 
platforms it is supposed to work (subtracting pointers on MS-DOS 
or on PDP-10 is not an easy proposition).
For simplicity sake D was defined to be implemented only on at 
least 32 bit machines with memory protection and linear address 
range.


More information about the Digitalmars-d mailing list