array initialization problem
    Chris Nicholson-Sauls 
    ibisbasenji at gmail.com
       
    Tue Jan 20 22:27:35 PST 2009
    
    
  
Rainer Deyke wrote:
> Denis Koroskin wrote:
>> Arrays in D are reference types. Besides, it's best to avoid hidden
>> allocations.
> 
> Arrays in D are reference types except when they're not.
> 
> int[] a = [5];
> int[] b = a;
> a[0] = 4;
> assert(b[0] == 4);
> a.length = 2;
> assert(b.length == 1);
> a[0] = 3;
> // Is b[0] 3 or 4?
> 
> 
To be really pedantic about it, D's arrays aren't really reference types 
at all, but bear the *illusion* of reference semantics because of what 
they really are (a struct with a length field and a pointer field).  In 
the above example, the value of b[0] depends on whether a was resized in 
place or not.  Which is why slicing, albeit a fantastically useful 
feature, has to be handled with care.
-- Chris Nicholson-Sauls <ibisbasenji @ Google Mail>
    
    
More information about the Digitalmars-d-learn
mailing list