Reality drives me to write less clean code, sorry for mistakenly posting in D.announce
Mike Parker
aldacron71 at yahoo.com
Fri Oct 12 00:25:26 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).
>
> 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.
Hmm. I could have sworn I had read something explicitly saying that any
pointer arithmetic was bad. Looking at it again, the only related items
I see are the following:
"Do not add or subtract an offset to a pointer such that the result
points outside of the bounds of the garbage collected object originally
allocated."
and
"Do not use byte-by-byte memory copies to copy pointer values. This may
result in intermediate conditions where there is not a valid pointer,
and if the gc pauses the thread in such a condition, it can corrupt memory."
Maybe there was something about this in the NG before? Or maybe I
interpreted the item above as ruling out the incrementing of pointers.
More information about the Digitalmars-d
mailing list