I forgot about Boxing with respect to adding value types to a weakly-typed
collection.

Regardless, it still seems like (at least with local variables) value-on-
stack reference-in-heap still applies, considering a boxed value type is a
reference.

There is some mention that member variables may not follow this rule,
which isn't surprising--especially for statics--but I haven't confirmed
that yet.

http://www.peterRitchie.com/

On Fri, 13 Jan 2006 08:28:52 +0100, Fabian Schmied <[EMAIL PROTECTED]>
wrote:

>> 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.

===================================
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