Allocating an empty non null associative arary

Steven Schveighoffer schveiguy at gmail.com
Tue Mar 31 14:54:19 UTC 2020


On 3/31/20 9:41 AM, Superstar64 wrote:
> On Tuesday, 31 March 2020 at 06:51:16 UTC, WebFreak001 wrote:
>> This doesn't only happen with null, you will notice that that function 
>> will eventually not add anything if you only add values. This is 
>> because by adding values you might reallocate the map at some other 
>> place in memory and because it's not ref, the caller won't have the 
>> map point to the new reference and still only have the old data.
> 
> Is the D spec wrong then? It says that associative arrays have reference 
> semantics. 
> https://dlang.org/spec/hash-map.html#construction_and_ref_semantic
> 

It does have reference semantics, but just like any reference type 
(pointer, class reference, etc), if it is initialized somewhere else, 
that initialization does not come back to the original unless you use 
ref semantics.

Easy to see with a pointer:

void foo(int *ptr)
{
    ptr = new int(5);
}

void main()
{
    int *p;
    foo(p); // does not affect p
}

-Steve


More information about the Digitalmars-d-learn mailing list