Big O Notation – How to Allocate Array of N Elements

allocationarraybig o

In Big O notation, allocate an array of N element is defined by O(1) or O(n) ?
For example in C#, if I allocate an array like this :

int[] a = new int[10]

When I display this array, I have :

{0,0,0,0,0,0,0,0,0,0}

Best Answer

Normally, size of an array has no effect on complexity of allocation itself. AFAIK, an array is internally a pointer to some address where the array begins and hidden fields for element size and element count. So it would be O(1).
At least this is the case in Object Pascal, though I am not sure for C# or other languages.

But finding the chunk of memory which fits for your array has some higher complexity which depends on the size at some point but is more dependent to how the active memory manager works: Time complexity of memory allocation.

Related Topic