Difference between Static Arrays and Dynamic Arrays
Last Updated :
20 Dec, 2023
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 a static 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.
C++
int a[5] = {1, 2, 3, 4, 5}; //Static Integer Array
int *a = new int[5]; //Dynamic Integer Array
Java
import java.util.*;
class GFG {
public static void main(String[] args)
{
// Static Array
int[] staticArray = new int[5];
int[] numbers = { 1, 2, 3, 4, 5 };
// Dynamic Array
// Create an ArrayList of integers.
ArrayList<Integer> dynamicArray = new ArrayList<>();
// 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 List
a_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 list
a_dynamic_alternative = list(range(5))
# Print the lists
print("Static List:", a_static)
print("Dynamic List:", a_dynamic)
print("Dynamic List (alternative):", a_dynamic_alternative)
# Coded By Block_Cipher
C#
using System;
class Program
{
static void Main(string[] args)
{
// Static List
int[] a_static = { 1, 2, 3, 4, 5 };
// Dynamic List (equivalent to dynamic array in some contexts)
int[] a_dynamic = new int[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 list
int[] a_dynamic_alternative = new int[5];
for (int i = 0; i < a_dynamic_alternative.Length; i++)
{
a_dynamic_alternative[i] = i;
}
// Print the lists
Console.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 array
static void PrintArray(int[] arr)
{
foreach (int item in arr)
{
Console.Write(item + " ");
}
Console.WriteLine();
}
}
JavaScript
// Static Integer Array
const staticArray = [1, 2, 3, 4, 5];
// Dynamic Integer Array (Array with size allocation)
const dynamicArray = new Array(5); // Allocating memory for 5 elements
// Initializing dynamic array elements
for (let i = 0; i < dynamicArray.length; i++) {
dynamicArray[i] = i + 1; // Assigning values 1, 2, 3, 4, 5
}
// Printing Static Integer Array
console.log("Static Integer Array:");
console.log(staticArray.join(" "));
// Printing Dynamic Integer Array
console.log("Dynamic Integer Array:");
console.log(dynamicArray.join(" "));
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:
|
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.
|