Difference between Static Arrays and Dynamic Arrays
Last Updated : 20 Dec, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report
In the Data structure, we know that an array plays a vital role in having similar types of elements arranged in a contiguous manner with the same data type. According to the concept of array, an array can be defined in two ways as per the memory allocation concept.
Types of Arrays:
There are basically two types of arrays:
Static Array: In this type of array, memory is allocated at compile time having a fixed size of it. We cannot alter or update the size of this array.
Dynamic Array: In this type of array, memory is allocated at run time but not having a fixed size. Suppose, a user wants to declare any random size of an array, then we will not use astatic array, instead of that a dynamic array is used in hand. It is used to specify the size of it during the run time of any program.
Example:
Let us take an example, int a[5] creates an array of size 5 which means that we can insert only 5 elements; we will not be able to add 6th element because the size of the array is fixed above.
importjava.util.*;classGFG{publicstaticvoidmain(String[]args){// Static Arrayint[]staticArray=newint[5];int[]numbers={1,2,3,4,5};// Dynamic Array// Create an ArrayList of integers.ArrayList<Integer>dynamicArray=newArrayList<>();// Add elements to the dynamic array.dynamicArray.add(1);dynamicArray.add(2);dynamicArray.add(3);// Remove an element from the dynamic array.dynamicArray.remove(1);}}
Python
# Static Lista_static=[1,2,3,4,5]# Dynamic List (equivalent to dynamic array in some contexts)a_dynamic=[0]*5# Initialize with zeros, similar to new int[5] in C++# Alternatively, you can use the list() constructor to create a dynamic lista_dynamic_alternative=list(range(5))# Print the listsprint("Static List:",a_static)print("Dynamic List:",a_dynamic)print("Dynamic List (alternative):",a_dynamic_alternative)# Coded By Block_Cipher
C#
usingSystem;classProgram{staticvoidMain(string[]args){// Static Listint[]a_static={1,2,3,4,5};// Dynamic List (equivalent to dynamic array in some contexts)int[]a_dynamic=newint[5];// Initialize with default values (0 for int), similar to new int[5] in C#// Alternatively, you can use a loop to initialize the dynamic listint[]a_dynamic_alternative=newint[5];for(inti=0;i<a_dynamic_alternative.Length;i++){a_dynamic_alternative[i]=i;}// Print the listsConsole.Write("Static List: ");PrintArray(a_static);Console.Write("Dynamic List: ");PrintArray(a_dynamic);Console.Write("Dynamic List (alternative): ");PrintArray(a_dynamic_alternative);}// Method to print an arraystaticvoidPrintArray(int[]arr){foreach(intiteminarr){Console.Write(item+" ");}Console.WriteLine();}}
The code mentioned above demonstrates the declaration and initialization of both a static integer array and a dynamic integer array. Let's break it down line by line:
Example of Static Array: int a[5] = {1, 2, 3, 4, 5};
A static integer array named a and initializes with the values 1, 2, 3, 4, and 5.
The size of this array is determined automatically based on the number of values provided in the initialization list.
Thus the array a is allocated on the stack, and its size cannot be changed once defined.
Example of Dynamic Array: int *a = new int[5];
In this code, we declare a pointer to an integer named a and it allocates memory in a dynamic fashion for an integer array of size 5.
The new keyword here is used for dynamic memory allocation, and int[5] specifies the size of this dynamic array.
The new operator is used to return the address of the dynamically allocated memory, which is already stored in the pointer a.
This array a is allocated on the Heap, and its size can be modified later if needed.
The differences between static and dynamic arrays based on this code snippet can be as followed:
Static Integer Array:
The size is determined automatically based on the number of values provided during initialization (in this case, 5).
The memory is allocated on the stack.
The size of the array is fixed once it is defined.
Dynamic Integer Array:
The memory is allocated during the run time by using the new keyword.
The size is specified explicitly (in this case, 5).
The memory is allocated on the heap (not the stack).
We can change the size of the array later by using delete[ ] which is used to to deallocate the memory and allocating a new block with a different size if desired.
Pictorial Representation of Static and Dynamic Arrays: Static Array and Dynamic Array
Key Difference between Static and Dynamic Arrays:
Static Array
Dynamic Array
1. The memory allocation occurs during compile time.
1. The memory allocation occurs during run time.
2. The array size is fixed and cannot be changed.
2. The array size is not fixed and can be changed.
3. The location is in Stack Memory Space.
3. The location is in Heap Memory Space.
4. The array elements are set to 0 or to empty strings.
4. The array elements can be destroyed during erase statement and the memory is then released.
5. This array can be Initialized but not erased.
5. This array cannot be read or written after destroying.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.