Arrays!

aayushii Gupta
Catalysts Reachout
4 min readOct 7, 2022

--

Many of us are not aware of the fact that array is a data structure.

YES, array is a linear data structure. It is most often used to build complex or composite data structures like stacks and queues. It is a collection or the systematic arrangements of objects of the same data type. Arrays stores the data in the contiguous and adjacent block of memory.

The typical definition of array would be:

(I preferably code in C++)

defining an array.

here int is the data type; arr is the name of the array; size is an integer which denotes the size of the array.

We can access the array elements as arr[0], arr[1],…arr[size-1]. The array indexing starts from 0 till (size-1).

We can access/ modify the array elements in O (1) time complexity (ie. In constant time).

HOW ARRAYS ARE STORED IN MEMORY?

Consider the below figure is the memory block, which is divided into these segments

Each segment is one byte of memory. Computer’s memory is a very large array of bytes. In computer architecture each byte of memory has an address. In C or C++, we have pointer variable that stores the address of another variable.

in memory.

Suppose we have an array

4 bytes of memory is allocated for an integer and we need memory for 3 integers then the memory allocation needed would be 3x4 =12 bytes of memory. When the program executes the complier allocated 12 bytes of contiguous memory for the array arr. First 4 bytes will corresponds to arr[0], similarly the next 4 bytes corresponds to arr[1] and so on.

The variable arr is a pointer to the first element in an array (that is the base address of the array).

Can you guess the output for the following?

The output would be the address, this is the base address of the array, that is the address from where the memory of the array is allocated.

O/P: 0x61ff04

The output would be the element at the 0th index.

Initialize the array with some values, or else you will get the garbage values.

Can you guess the output?

The output would be

O/P: 0x61ff08

arr is pointer to an integer, then arr+1 would increment the address by 4 bytes, it gives the address of the second element of the array arr.

The output would be the element at the 1st index.

We can traverse the entire array using loops.

using a while loop

Advantages of Arrays:

  • Arrays store multiple elements of the same type with the same name.
  • You can access array elements randomly by using an index number.
  • Array- Memory is predefined, so there is no additional memory leak.
  • Arrays prevent memory overflow.
  • 2D arrays can efficiently represent tabular data.

Disadvantages of array:

  • The number of elements in an array must be predefined
  • An array is static. You cannot change its size after declaration.
  • The operation of inserting and deleting an array is quite complicated because the array continuously stores elements.
  • Allocating excess memory than necessary may result in wasted memory.

To get comfortable with arrays you can check the following link, a link to GitHub repository , where I’ve added some resources.

Thanks for reading! If you have any queries feel free to drop an comment.

--

--