Declaration and Allocation of Memory For Arrays: Arrays: What's An Array?
Declaration and Allocation of Memory For Arrays: Arrays: What's An Array?
Declaration and Allocation of Memory For Arrays: Arrays: What's An Array?
What’s an Array?
Arrays are vital for most programming languages. They are collections of variables, which we call elements.
An array’s elements in C# are numbered with 0, 1, 2… N-1. Those numbers are called indices. The total number of
elements in a given array we call length of an array.
All elements of a given array are of the same type, no matter whether they are primitive or reference types. This
allows us to represent a group of similar elements as an ordered sequence and work on them as a whole.
Arrays can be in different dimensions, but the most used are the one-dimensional and the two-dimensional
arrays. One-dimensional arrays are also called vectors and two-dimensional are also known as matrices.
In C# the arrays have fixed length, which is set at the time of their instantiation and determines the total number
of elements. Once the length of an array is set we cannot change it anymore.
Declaring an Array
int[] myArray;
In this example the variable myArray is the name of the array, which is of integer type (int[]). This means that
we declared an array of integer numbers. With [] we indicate, that the variable, which we are declaring, is an
array of elements, not a single element.
When we declare an array type variable, it is a reference, which does not have a value (it points to null). This is
because the memory for the elements is not allocated yet.
The figure below shows how a declared array variable looks, when the memory for elements of the array is not
allocated yet:
In the program’s execution stack the variable with the name myArray is created and its value is set to null
(meaning it holds no value).
In C# we create an array with the help of the keyword new, which is used to allocate memory:
In this example we allocate an array with length of 6 elements of type int. This means that in the dynamic
memory (heap) an area of 6 integer numbers is allocated and they all are initialized with the value 0:
The figure shows, that after the allocation of memory for the array the variable myArray points to an address in
the dynamic memory, where the values are. In C#, the elements of an array are always stored in the dynamic
memory (called also heap).
During the allocation of the memory for an array we set the total number of the elements in the brackets (a non-
negative integer number), defining its length. The type of the elements is written after the reserved word new, so
we indicate what type of elements are going to be allocated in the memory.
Before we can use an element of a given array, it has to be initialized or to have a default value. In C# all variables,
including the elements of arrays have a default initial value. This value is either 0 for the numeral types or its
equivalent for the non-primitive types (for example null for a reference type and false for the bool type).
Of course we can set initial values explicitly. We can do this in different ways. Here is one of them:
int[] myArray = { 1, 2, 3, 4, 5, 6 };
In this case we create and initialize the elements of the array at the time of the declaration. On the figure below
we see how the array is allocated in the memory when its values are initialized at the moment of its declaration:
With this syntax we use curly brackets instead of the operator new. Between the brackets we list the initial values
of the array, separated by commas. Their count defines the length of the array.
In this case we allocate an array of seven elements of type string. The type string is a reference type
(object) and its values are stored in the dynamic memory. The variable daysOfWeek is allocated in the stack
memory, and points to a section of the dynamic memory containing the elements of the array. The type of each of
these seven elements is string, which itself points to a different section of the dynamic memory, where the real
value is stored.
myArray[index] = 100;
In the example above we set a value of 100 to the element, which is at position index.
myArray[20] = 100;
In the example above we set a value of 100 to the element, which is at position index 20
Here is an example, where we allocate an array of numbers and then we change some of them:
As we can see, all elements, except those for which values are explicitly set, are initialized with the value 0 when
the memory of the array was allocated.
We can iterate through the array using a loop statement. The most common form of such iteration is by using a
for-loop:
int[] arr = new int[5];
for (int i = 0; i < arr.Length; i++)
{
arr[i] = i;
}
Again we use a loop to iterate through the array. At each iteration we set the current element to what we have
read from the console. The loop will continue n times, which means it will iterate through the array and so we will
read a value for each element of the array:
Element[0] = one
Element[1] = two
Element[2] = three
Element[3] = four
Iteration through Elements of an Array
As we can see, the iteration through the elements of an array is one of the most used techniques when we work
with arrays. Consecutive iterating using a loop will allow us to access each element through its index and we will
be able to modify it as we want. We can do that with different loop constructs, but the most appropriate loop is
the for-statement. We will examine in details how this type of iteration works.