Re: Array.Clear vs List<>.Clear

Tech-Archive recommends: Speed Up your PC by fixing your registry



"Willy Denoyette [MVP]" <willy.denoyette@xxxxxxxxxx> wrote in message news:%23oPFVRrBIHA.4956@xxxxxxxxxxxxxxxxxxxxxxx
"Doug Semler" <dougsemler@xxxxxxxxx> wrote in message news:1191519587.047316.183650@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
On Oct 4, 11:54 am, "Laura T." <LT_s...@xxxxxxxxx> wrote:
"Lee Crabtree" <lcrabt...@xxxxxxxxx> ha scritto nel messaggionews:ul6KAxpBIHA.4476@xxxxxxxxxxxxxxxxxxxxxxx

> The major reason I use a List<T> in a place where an Array would > normally
> be more common is that the line length has to be extended or shortened > at
> certain times, and recreating the array then copying elements across > seems
> unnecessarily ridiculous.

Well, that's what you are going to get with List<T> anyway, copying and more
(ridiculous) copying.
As Vadym said, the List<T> internally manages the list as an Array,[], so
every time you add one element over the List<T>.Capacity, the it will create
a new array, copy the elements, add the new item and then destroying the old
array.
The unfortunate thing with List<T> is that the capacity does grow up one by
one. I'd like to see something like "expand it by 10%", so that the next Add
would not incur reallocations.

It *doubles* the internal array size whenever you add an item that
would grow the array, not one by one. Starting at whatever your
initial capacity was (4 is default).



No Laura is right, a new array is created with a size which is equal to the previous size + 1 (size is an int) whenever there is an overflow.
Consider following sample:

List<byte> testList = new List<byte>();
for(int i = 0; i < 8; i++)
testList.Add((byte)i);
Here the List starts with an array of size 0, at the first insert a new array gets created with a size of 1 (int), that means that we can add 4 bytes before the array overflow, when that happens a new array gets created with a size of 2, and again 4 bytes can be added.
For the above sample that means creating 3 array's :-(

Willy.



I see I'm not (completely) right of course, point is that the default size is zero, and is doubled when there is an overflow.

So in the previous sample, the sequence is:
array size 0
add 1 byte -> create array with size 1 (int) , say n
add byte
add byte
add byte
add byte -> create array with size n = n* 2, from now on the new array is doubled in size.
....

Sorry for the confusion.
Willy.

.



Relevant Pages

  • Re: [PATCH v3 2/2] bsearch: prevent overflow when computing middle comparison element
    ... overflow situation when computing the middle element for comparison. ... maximum int value. ... approaches the maximum unsigned int ... at the highest extreme of the array. ...
    (Linux-Kernel)
  • Re: Segmentation Fault
    ... You shouldn't assume that you will be able to allocate an array this ... i*i gives overflow and overflow of int is not defined ... Visit http://www.ecomstation.de the home of german eComStation ...
    (comp.lang.c)
  • Re: Pointer arithmetics in 2D arrays
    ... so it becomes int *. ... are, strictly speaking, breaking the rules. ... overstepping the bounds of an array. ... overflow was declared UB in the first place (i.e. speeding up run time by ...
    (comp.lang.c)
  • Re: A portion of long data bytes as a property
    ... copying the array isn't really that big of a problem most of the time; usually that copy is going to be sent off somewhere that is WAY slower than memory access. ... byteEntireData; ... actionUse(EntireBody, HeaderSize, EntireSize - HeaderSize); ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Garbage collectable pinned arrays!
    ... allocation of the same amount of memory. ... int allocationOnly = TimeAllocation; ... copying were involved, the numbers would go up linearly with the size, ... to manually set the values of the byte array in the pinning version ...
    (microsoft.public.dotnet.languages.csharp)