Reality drives me to write less clean code, sorry for mistakenly posting in D.announce

Don Clugston dac at nospam.com.au
Fri Oct 12 00:42:12 PDT 2007


Mike Parker wrote:
> Bill Baxter wrote:
>> Mike Parker wrote:
>>> davidl wrote:
>>>>
>>>> First practice:
>>>>
>>>> char[] text;
>>>> char* p;
>>>> char* end;
>>>>
>>>
>>> Pointer arithmetic in D is a no-no, unless you are using memory 
>>> allocated outside of the GC. 
>>
>> That's news to me.  There's nothing wrong with using pointers, it's 
>> just more verbose and error prone than foreach.
> 
> Of course there's nothing wrong with /using/ pointers, but pointer 
> arithmetic is one item in a list of 'Undefined Behavior' regarding 
> pointers to GC memory. See the page 
> http://www.digitalmars.com/d/garbage.html (section titled Pointers and 
> the Garbage Collector).

I can't see anything about pointer arithmetic in that section. Did I miss it?
As I read it, the basic rule is that pointers must stay as pointers at all 
times, and you can't subtract pointers that point to unrelated objects.
Pointers that are pointing to different parts of the same block of memory (eg 
the same array) could be changed by the gc, but they'll all be moved in the same 
way, so everything should still be OK.

> 
> This isn't really an issue in the present since the current GC doesn't 
> move anything around, AFAIK. But it's still a bad habit to get into (or 
> a good one to get out of for people with C backgrounds) and doesn't do 
> much for future-proofing.



More information about the Digitalmars-d mailing list