Array operations and 'Copy on Whatever'
kovrov at no.spam
Thu Aug 3 16:15:20 PDT 2006
In my case it's more like 'copy on delete' then COW, but anyway... How
array concatenation, assignment, etc. are handled? Is it implemented on
'library level', or on 'language level'? I mean some operations silently
copies array contents, but some are not (eg. referencing). And I do not
see a pattern.
In particular, did array concatenation (~, ~=) make a copy of right operand?
Consider this example:
> char array_of_strings;
> for (int i = 0; i < 10; i++)
> char tmp = format("this is %d", i);
> array_of_strings ~= tmp;
> delete tmp; // <-- here
> foreach (char str; array_of_strings)
> writefln("%s", str);
When I delete tmp, strings in array_of_strings being trashed.
But on the other hand, in more simple example:
> char s1 = "one";
> char s2 = "two";
> s1 ~= s2;
> delete s2;
Deleting s2 will not affect s1.
The first one is much simplified example reproducing a problem I have
encountered some time ago. As I do not understand how array operations
works and cant rely them, I have to .dup near every string involved.
Which seems no very smart.
So, Copy on Write, what does it mean in context of D/Phobos implementation?
More information about the Digitalmars-d-learn