Arrays in C/C++

Harshvardhan Aditya
3 min readNov 3, 2021

--

Introduction

Let’s talk about the array data structure. What are arrays? An array is a collection of homogeneous items stored in a contiguous block of memory. What does contiguous mean? It means that they are in sequence. So arrays are stored something like-

http://www.mathcs.emory.edu/~cheung/Courses/170/Syllabus/09/FIGS/array02x.gif
How arrays are stored in memory

How to access elements in an array

Since arrays are a group of homogeneous datatypes, they can be accessed at constant time complexity that is O(1). Why so? Because the amount of memory required by each datatype is constant. So if you have created an array of integers then it’s a given that each integer takes 4 bytes of memory(this is a generalization). Therefore, since your compiler already knows the address of the first element, for all the remaining elements you just have to add 4(bytes to your memory address). So if the memory of the first element of an array of size 10 is 227 then the memory address of the second element will be 231, the third will be 235, the tenth will be 263. So if we consider the index(i) starts from 0

  • First element index — 0
  • Second element index — 1
  • Third element index — 2 and so on

then the location of the ith element will be (227+4*i).

Here is a similar example —

https://www.log2base2.com/images/ds/linear-array-ds.png

Some constraints of arrays

The size of an array is constant. Therefore you cannot increase the size of a given array. Why is that so? Well, arrays are created during compile time therefore during creation the compiler asks for some memory to be allocated for storing the array from the stack memory. Therefore its size is fixed. If you want to add another value to the array then you’ll have to create a larger array and copy all the elements from the first array to the new array and also delete the previous array as well as assign the pointer variable to the new array. This entire process would be a tedious task. Also, the fact that you can’t store different types of data types. Insertion and deletion operations are non-existent since arrays are contiguous so what we could do is assign them a value that they probably would never reach such as INT_MIN or INT_MAX.

Finally, implementation of arrays —

#include <iostream>using namespace std;int main(){int arr[5]={10,20,30,40,50};cout<<arr[2];}

Output-

30

Okay, this is like the shortest implementation I could’ve made but I’ll keep adding and updating this post in the future. If you feel like I missed a point or was wrong about something, then please let me know through the comments.

Thank you for reading and I hope you learned something from it.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response