Static Associative Arrays
Jonathan M Davis
jmdavisProg at gmx.com
Sat Apr 7 23:31:30 PDT 2012
On Sunday, April 08, 2012 01:24:02 Caligo wrote:
> On Sat, Apr 7, 2012 at 11:01 PM, Jonathan M Davis <jmdavisProg at gmx.com>
wrote:
> > What do you mean my static associative arrays? Are you asking why you
> > can't
> > initialize a static variable which is an AA at compile time? e.g.
> >
> > - Jonathan M Davis
>
> The same way I can create a static array:
>
> int[4] = [1, 3, 4, 8]; // has value semantics
>
> and dynamic arrays:
>
> int[] = [1, 4, 2, 4]; // has reference semantics
>
> I want an associative array that has value semantics and it's size
> doesn't change, just like static arrays.
Associative arrays are always on the heap. They always have reference
semantics. It would be very expensive to have an AA with value semantics. They
contains pointers all over the place. It would be equivalent to calling dup on
them every time that you pass them to anything. And trying to put one on the
stack would get very messy because all of the pointers involved. AAs are
_completely_ different from dynamic and static arrays. Aside from the fact that
they both have array in their name and both allow indexing of a sort, there's
really no relation between them at all.
> P.S.
> another point. I was always under the impression that static arrays
> are allocated on the stack whereas dynamic arrays are allocated on the
> heap and the GC cleans them up. After today, I'm not so sure if this
> is true. Are static arrays allocated on the stack? if so, that would
> be another reason to want to have static associative arrays.
Yes. static arrays go on the stack and are value types, whereas dynamic arrays
go no the heap and are reference types which are managed by the GC. If you
want more details on how arrays work, read this:
http://dlang.org/d-array-article.html
- Jonathan M davis
More information about the Digitalmars-d-learn
mailing list