Open In App

How Array is stored in different programming languages?

Last Updated : 28 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The memory representation of an array mainly depends on various programming languages like C, C++, Java, Python, JavaScript, C#, and PHP. So we look at how an array is stored in several popular programming languages :

How is Array stored in Memory in C and C++?

  • In C and C++, arrays are stored as contiguous blocks of memory.
  • The elements are stored consecutively in memory, and we can access elements using pointer arithmetic.
  • The size of the array and the data type of its elements determine how much memory is allocated.

C




int arr[5] = {1, 2, 3, 4, 5};
// Memory representation:
// | 1 | 2 | 3 | 4 | 5 |


How is Array stored in Memory in Java?

  • In Java, Memory representation is similar to C/C++.
  • In Java, arrays are objects, and the elements are stored as consecutive memory locations.
  • Java abstracts memory management, so we don’t need to worry about pointer arithmetic.
  • The Java Virtual Machine (JVM) takes care of memory management.

Java




int arr[5] = {1, 2, 3, 4, 5};
 
// Memory representation is same for c/c++:
// | 1 | 2 | 3 | 4 | 5 |


How is Array stored in Memory in Python?

  • Python use complex data structure to represent arrays called lists.
  • Lists are dynamic arrays, and each element is an object reference.
  • In lists elements can be of different data types.
  • In lists the memory layout is not necessarily to be contiguous.

Python




arr = [1, 2, 3, 4, 5]
 
# Memory representation : (Not contiguous due to object references. )


How is Array stored in Memory in JavaScript?

  • In JavaScript, arrays are implemented as objects.
  • JavaScript are dynamic and can hold elements of different types.
  • Elements are stored in a way that allows for random access, but they are not necessarily stored in contiguous memory.

Javascript




var arr = [1, 2, 3, 4, 5];
 
// Memory representation: Not necessarily contiguous due to object properties.


How is Array stored in Memory in C#?

  • C# arrays are similar to those in C and C++.
  • Memory representation is similar to C/C++.
  • They are stored as contiguous blocks of memory, and you can use pointer arithmetic, but this is often abstracted by C# itself.

C#




int[] arr = {1, 2, 3, 4, 5};
 
// Memory representation is similar to C/C++.


How is Array stored in Memory in PHP?

  • PHP arrays are implemented as hash tables, allowing for flexibility with keys and values.
  • The memory representation is not a contiguous block of memory like in C or C++.

PHP




$arr = [1, 2, 3, 4, 5];
 
// Memory representation: Hash table-like structure for keys and values.


The memory representation of arrays of varies in programming languages. Some languages use contiguous memory blocks, while others use more complex data structures to allow for dynamic sizing and different data types within the array. Understanding how arrays are stored in memory is important for optimizing code and managing memory efficiently in low-level languages like ( C and C++) , but it is complex in higher-level languages like Python, JavaScript, and PHP, where the languages handle memory management at runtime.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads