Is this a desing rationale? (static array object member)
downs
default_357-line at yahoo.de
Sat Sep 29 01:15:01 PDT 2007
Brian Hsu wrote:
[snip]
> So a.z and b.z pointed to same array, but int [] y and int [] z are still different array instance.
>
> Regan mentioned that this is because the array literal [1,1,1,1,1] create only one instance of array*, so at first time I suspect that when compiler see [1,1,1,1,1], it would translate that to a fixed memory address of something like that.
>
The reason is that both a.z and b.z were initialized with the _same_
array literal, whereas y and z were initialized with _different_ array
literals.
> Finally, is this behavior reasonable? Since I didn't declare that int [] as a static class member, even though they have same initialization array literal, but I would expect that a.z/b.z they should be different array have same content. (As in Java or C++)
That's exactly what they are.
Different arrays with the same content.
Of course, it helps to know that in D, an array is basically this:
struct array(T) {
T *ptr;
size_t length;
}
So as you see, the memory area that the array uses _is_ the content :)
> Or is there special reasons of this strange behaver?
It's really quite consistent, once you understand what arrays _are_ in D.
--downs
More information about the Digitalmars-d
mailing list