Get Your Arrays Concept Clear

Divyansh Garg
2 min readJul 14, 2022

Arrays :
It is a data structure used for storing data of the same data type. For eg: If you need to hold the age of 200 people you need 200 different variables to keep them… it is very difficult to handle and maintain this number of variables. So here comes the arrays to store many people's data in just one variable name.

Some IMP properties of Arrays :

  1. FIXED-SIZE :
    Arrays have fixed sizes. Once u declared the array size u can’t modify the size in your further program.
  2. Elements of Same Data Type :
    If the data type of the array is Int then all the elements present in the array should be of the same data type. If not it would give u an error while compiling the code.
  3. Contiguous Memory Allocation :
    when u create an array then the number of blocks(which is the size of the array) are in contiguous blocks i.e if the address of the start block is 0x0000 and the size is 5 then the last block address will be 0x0005.
  4. Indexing Starts from Zero :
    Mainly in languages like java and CPP, the indexing starts from zero i.e the first block of the memory allocated to the array is zero and the last one is n-1(where n is the size of the array).

How are Arrays declared in programs :

On the Stack Memory :

int arr[n];  //--- where "int" is data type "arr" is variable name and n is the size of the array.

On the Heap Memory :

int* arr = new int[n];//-- "new" variable is used to declare dynamic space . Square brackets after int is used to declare the size.

Differences in Arrays created in Stack and Heap Memory :

When the array is created in the stack the space given to that array is in stack memory and once the function is completed then the variable is destroyed and memory is reclaimed in the case of heap memory the programmer is responsible to delete the memory but if it doesn't delete the memory that is called a memory leak.

By: Divyansh Garg
3rd year CSE Student

--

--