If you haven’t learned data structures in your coding journey, I’m still pretty sure that, unknowingly, you have already used one of the data structures in your code. Yes, I am talking about Arrays, one of the most commonly used data structures.

An array is a basic data structure used to store a collection of items that have the same data type. It is stored in a sequential pattern in computer memory, allocating adjacent locations to each individual item of the array. By choosing an array as a data structure to store a collection of items, we can do the following operations with them:

  • Traversal → Accessing each element in the array
  • Insertion → Inserting new elements or updating existing ones in the array
  • Deletion → Deleting an existing array element
  • Search → Looking up a particular element in the array
  • Sort → Sorting array elements in a required way

Defining an array

int arr[10];

By writing the above code, you can simply define an array in your program (We are using C++ as an example). Let’s break down what the above code means and what happens when we write it in our code.

Firstly, the word “int” defines the data type of all the elements that will be kept inside this array. By writing “int” we are defining that all the data types of the array elements will be integers. Then comes the name of the array, and here we named our array “arr.” Just like your parents gave you a name so that you can be called by others, we gave a name to our array so that we can call it later in our code. Then we can see that inside a bracket we have placed a number in our above code. Here, the number 10 defines the size of our array. It means our array can hold up to 10 items, but no more than that. To summarize, the above code means that we now have an array named arr that can keep up to 10 integer elements inside it.

Traversal

After defining an array, the question that comes to our mind is how we are going to access the different items in the array. The answer is using indexes. The counting of indexes starts from 0, so if we have to access the first element of the array arr, we have to give the index number inside the bracket just like arr[0]. This gives us a clear idea of how to traverse array elements. To conclude, if we have to access an element at position 6, we will have to write the following code, giving 5 as an index to the array arr[5];.

Memory Allocation

Once we run our code, it allocates memory for our defined array in a sequential pattern. Now the question that arises is, how much space does this array take up in our memory? A single integer takes 4 bytes in memory, and we have defined our array with a size of 10, which means that if one integer element needs 4 bytes, our array of 10 integers will need 10 * 4 = 40 bytes. If we had instead created an array of characters instead of integers and given them a size of 10, then they would have taken 10 bytes in memory because 1 character takes 1 byte in memory, so an array of 10 characters will take 10 bytes in memory.

All the memory will be taken up in a sequential pattern. For example, let’s imagine that the first element of our array arr is at address 100; then the next element would be at address 104. This is because our array arr is an integer array, and we know that an integer takes 4 bytes, for which the first element will take the blocks of 100, 101, 102, and 103 in memory, and the second element will take 104, 105, 106, and 107 in memory, and this goes on for other elements too. From the beginning, we have been repeating that the array stores elements in a sequential pattern, so it is not possible that our first element will be at address 100 and the next right element in the array will be at an address somewhere around 500. From this idea, we can deduce a formula that if the address of the first element in the array is B and we need to find the address of the element at index i, the formula will be → B + i * data size, where data size means how much memory one data type takes in memory, like an integer takes 4 bytes, a character takes 1 byte, etc. Thus, if we were asked to find the address of an element at index 3 of our array arr, where the first index is at address 100, then that would be 100 + 3 * 4, which is 112.

Happy Coding👨‍💻

Categorized in:

DSA,