Pointer to variables in D

H. S. Teoh hsteoh at quickfur.ath.cx
Wed Apr 25 22:17:53 PDT 2012


On Thu, Apr 26, 2012 at 06:57:50AM +0200, EraScarecrow wrote:
> On Thursday, 26 April 2012 at 03:54:25 UTC, Nathan M. Swan wrote:
> >Yes. My rule: don't use pointers unless doing it otherwise is
> >really slow or really hard.
> 
>  Might be safe to use a pointer on an immutable struct (Say,
> globally accessible data?). I'm trying this; but it's only to get
> around the need to copy the variable when in fact since the data
> never changes why should I use more space by copying it?
> 
>  Not the best idea, but if the data never changes, memory isn't
> manually managed, don't do pointer arithmetic, then it should be
> safe (Long as it's not local variable data).
> 
>  That said, I agree, avoid pointers as much as you can.

I don't know about deliberately *avoiding* pointers, but D's awesome
features are such that you hardly ever *need* to use pointers. There are
some situations where it's called for; there's nothing wrong with using
it in those cases. Besides, D's auto-deferencing semantics on pointers
makes the difference barely noticeable anyway. Pointers auto-deference
when using '.' member notation, for example. Once in a while you have to
explicitly dereference a pointer or take an address, but those are very
rare.


T

-- 
A mathematician is a device for turning coffee into theorems. -- P. Erdos


More information about the Digitalmars-d-learn mailing list