Sorry, I should have been more clear.  When I said "collections" I didn't
mean arrays, I meant classes derived from classes within
System.Collections.


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)

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);
 }

If you're processing lines of a file you're more likely to use a
Collections based class than a language-supplied array.

http://www.peterRitchie.com/

On Thu, 12 Jan 2006 22:10:07 +0100, Fabian Schmied <[EMAIL PROTECTED]>
wrote:

>> Value types (structs) are intended to be used where a lightweight (few
>> members, little instance data, generally do not persist for long) type
is
>> required.  It's also not well designed for huge collections of data.
>> Value types are allocated from the stack rather than from the heap.  So,
>> for large collections of large value types you need to consider the
stack
>> ramifications.
>
>Value types are only allocated on the stack in some cases
>(http://www.pobox.com/~skeet/csharp/memory.html).
>
>"Collections of value types" are indeed typically not allocated on the
>stack, so you don't need to consider the stack ramifications there.
>(Unless you are talking about collections in the sense of a large
>number of local variable declarations, but I don't think you will
>easily run into stack problems with local variable declarations.)
>
>In particular, in code similar to the following, the array will not be
>allocated on the stack, even if array is a local variable:
>
>int[] array = new int[10000];
>
>The same applies with newly defined value types:
>
>struct S {
>}
>
>S[] array = new S[10000];
>
>IIRC, this can be much more memory efficient than the equivalent:
>
>class C {
>}
>
>C[] array = new C[10000];
>for (int i = 0; i < 10000; ++i) {
>  array[i] = new C();
>}
>
>The struct way is more runtime efficient, too, because value types can
>be initialized without their constructors being run [1].
>
>Fabian
>
>[1] The public default constructor of value types can be simulated by
>filling a block of memory with zeroes.

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