0

In C# when you create an array, it is initialized with default value for each element.

var array = new int[1000];
// array elements are initialized with 0 by default.

Although it is a very good behavior which prevents lots of bugs, but in some situations initializing elements by default value may be unnecessary and lead to some performance pitfalls. For example:

while (someCondition)
{
    var array = new int[10000];
    for (var i = 0; i < 10000; i++)
        array[i] = someMethod(i);
}

In this scenario the initialization of array is totally unnecessary and in some cases may cause performance issues.

Question: Is there anyway to create an array not initializing its elements by some value (so each element has some random number in it)?

Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
mehrandvd
  • 8,806
  • 12
  • 64
  • 111
  • Any initilization of array will require `O(n)` (linear). No way to avoid it. – pwas Oct 21 '15 at 11:10
  • 1
    This is a duplicate question. Answer is no, you can't. – Sriram Sakthivel Oct 21 '15 at 11:11
  • And thank goodnes that you can't - the OS won't let you. That's one of the oldest hacks to read other processes' memory, which is why the OS will always give you clean memory pages. Which also means - no there is no performance penalty – Panagiotis Kanavos Oct 21 '15 at 11:14
  • In any case an array initialization has a O(n) complexity. You can create a nullable int array and set value to null to avoid bug, but in any case the complexity will be the same. var array = new int?[10000]; for (var i = 0; i < 10000; i++) array[i] = null; – Nicolas HENAUX Oct 21 '15 at 11:21
  • @PanagiotisKanavos If it's OS that clears memory, so it shouldn't be possible to get initialized array in C or C++ too. – mehrandvd Oct 21 '15 at 11:29
  • @mehrandvd it depends on the system calls used underneath. In fact, even in very old C++ versions, whether the memory is initialized or not is controlled by a switch. This is also indirectly mentioned in the linked duplicate – Panagiotis Kanavos Oct 21 '15 at 11:35
  • In fact, it's a bit more complicated - the OS clears pages in the background so you can never get another process's discarded pages. Uninitialized pages can only come from the same process's heap. Morever, the warnings on LocalAlloc (the API function behind Marshall.AllocHGlobal) warn that calling the actually *introduces* a performance penalty – Panagiotis Kanavos Oct 21 '15 at 11:44
  • 1
    @mehrandvd what is the *actual* problem you are trying to solve? Instead of avoiding initiallization, perhaps you should look at *reusing* buffers by using [BufferManager](https://msdn.microsoft.com/en-us/library/system.servicemodel.channels.buffermanager(v=vs.110).aspx). Another option is to declare the buffer *outside* the loop to avoid repeated object creation. This is costly even if the buffer data isn't initialized – Panagiotis Kanavos Oct 21 '15 at 11:49
  • @PanagiotisKanavos Wow, reusing buffers, That's it. I'm so stupid that I haven't thought about it in this way. Thanks. It was better to ask about my problem, not ask about my answer to the problem! – mehrandvd Oct 21 '15 at 11:55

0 Answers0