> Also, I was under the impression that C# simply mapped array definitions
> to the Array class.  So:
>
> S[] array = new S[10000];
>
> is directly allocating a reference type.  I was also under the impression
> that the Array type managed its own memory (e.g. in the above example it
> isn't allocating 10000 individual value types)

This is true, S[] is a reference type, but the separate instances of S
are allocated on the heap in a contiguous block of memory (this is
just from my memory now, I'd have to look it up to be certain on how
it works).

> I assume the value types in this example are allocated from the stack:
>  ArrayList al = new ArrayList();
>  for(int i = 0; i < 10000; i++)
>  {
>  al.Add(i);
>  }

Well, yes and no. Of course, i is a local variable and its int value
lives on the stack. But when i is reassigned, the old value is simply
overwritten, so there is always just one integer object in the stack.
When the value of i is added to the ArrayList, it is boxed, i.e.
copied to the heap. So, there really are 10000 boxed int objects plus
one contiguous block of object references on the heap after this loop,
which is neither memory nor runtime efficient.

With List<int>, however, I'd guess there would be a backing int[], so
it should be very efficient again.

Fabian

===================================
This list is hosted by DevelopMentorĀ®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to