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

norm norm.rowtree at gmail.com
Fri Apr 1 23:08:09 UTC 2022


On Friday, 1 April 2022 at 21:20:00 UTC, H. S. Teoh wrote:
> On Fri, Apr 01, 2022 at 01:56:19PM -0700, Ali Çehreli via 
> Digitalmars-d wrote:
>> On 4/1/22 13:47, Steven Schveighoffer wrote:
>> 
>> >> 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.
>> >
>> > Well, can the pointers be subtracted? Yes.
>> 
>> My understanding is that depending on the CPU, certain 
>> operations would make the CPU barf. I am not sure but the old 
>> protected memory, extended memory, etc. systems might not be 
>> able to subtract between the systems at all. (Not sure; I am 
>> making this up.)
> [...]
>
> In the bad ole days of segmented protected memory (around the 
> days of the 386 or 486, IIRC), you could have memory with 
> different segment prefixes, referenced using a convoluted 
> scheme of far ptrs and near ptrs. Near ptrs are relative to a 
> particular segment; subtracting near ptrs associated with 
> diverse segment pointers yields nonsensical values. You can 
> subtract far ptrs, sorta-kinda, but the results are likely to 
> be either garbage, or else refer to an address that can't be 
> addressed with existing segment pointers. So basically, it's 
> Trouble with a capital T.
>
> On modern machines, though, this is no longer relevant. I think 
> people figured out real quick that segmented addressing is just 
> way more trouble than it's worth, so we came running back, tail 
> between legs, to the flat memory model and embraced it like 
> there's no tomorrow. :-D
>
>
> T

Ahh the "good" old days were not that good really when it came to 
addressing :-)

All x86 still start up in real mode and the "flat" modes 
(protected mode, long mode etc.) are still reached by SW putting 
the CPU into that mode. Today that happens in your boot loader as 
all desktop SW pretty much runs in 64bit flat mode but back in 
the 90's most SW would have to manage this itself.

There is an old, yet still interesting, blog about Win95 and the 
world as it was when we were all slowly migrating from 16bit real 
mode DOS to 32 bit flat mode OS's:

https://devblogs.microsoft.com/oldnewthing/20071224-00/?p=24063
(Any blog by Raymond Chen is well worth a read IMO, I always 
learn something new)

Sorry my post has gone completely off topic.


More information about the Digitalmars-d mailing list