Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Direct array initialization with a constant value

Whenever you allocate a new array in C# with

new T[length] 

the array entries are set to the default of T. That is null for the case that T is a reference type or the result of the default constructor of T, if T is a value type.

In my case i want to initialize an Int32 array with the value -1:

var myArray = new int[100]; for (int i=0; i<myArray.Length; i++) { myArray[i] = -1; } 

So after the memory is reserved for the array, the CLR loops over the newly allocated memory and sets all entries to default(int) = 0. After that, my code sets all entries to -1.

That makes the initialization redundant. Does the JIT detect this and neglects the initialization to 0 and if not, is there a way to directly initialize a portion of memory with a custom value?

Referring to C# Array initialization - with non-default value , using Enumerable.Repeat(value, length).ToArray() is no option, because Enumerable.ToArray allocates a new array and copies the values to it afterwards.

like image 374
Rauhotz Avatar asked Mar 15 '09 23:03

Rauhotz


People also ask

How do you initialize an entire array with a single value?

Initializer List: To initialize an array in C with the same value, the naive way is to provide an initializer list. We use this with small arrays. int num[5] = {1, 1, 1, 1, 1}; This will initialize the num array with value 1 at all index.

Can you initialize an array with a variable?

You initialize an array variable by including an array literal in a New clause and specifying the initial values of the array. You can either specify the type or allow it to be inferred from the values in the array literal.

What are the different ways of initializing array?

There are two ways to specify initializers for arrays: With C89-style initializers, array elements must be initialized in subscript order. Using designated initializers, which allow you to specify the values of the subscript elements to be initialized, array elements can be initialized in any order.

Is initializing an array constant time?

you can initialize an array with a constant value in O(1) time. but it requires extra memory. Actually a better algorithm is known, such that the extra 2n memory words are not needed anymore.


2 Answers

Similar to Dan's answer but without the need of using collections:

int[] myArray = Enumerable.Repeat(-1, 100).ToArray(); 
like image 82
Taylor Leese Avatar answered Sep 23 '22 19:09

Taylor Leese


It's not redundant.

Suppose an exception is thrown during your initialization loop. If the CLR hasn't cleared the memory first, you might be able to "see" the original uninitialized memory, which is a very bad idea, particularly from a security standpoint. That's why the CLR guarantees that any newly allocated memory is wiped to a 0 bit pattern.

The same argument holds for fields in an object, by the way.

I suppose in both cases the CLR could check that you're not going to make the array visible elsewhere before finishing initialization, but it's a complicated check to avoid a pretty simple "wipe this area of memory".

like image 38
Jon Skeet Avatar answered Sep 21 '22 19:09

Jon Skeet