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.
|
Similar Reads
Difference between Stack and Array Stack: A stack is a linear data structure in which elements can be inserted and deleted only from one side of the list, called the top. A stack follows the LIFO (Last In First Out) principle, i.e., the element inserted at the last is the first element to come out. The insertion of an element into a
3 min read
Difference between Array, Queue and Stack Array: An Array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of the same type together. This makes it easier to calculate the position of each element by simply adding an offset to a base value, i.e., the memory location of the first element of
3 min read
Difference between Array and Map Array:An array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of the same type together. This makes it easier to calculate the position of each element by simply adding an offset to a base value, i.e., the memory location of the first element of t
12 min read
Difference between Array and String in Java An array is a collection of similar type of elements that are stored in a contiguous memory location. Arrays can contain primitives(int, char, etc) as well as object(non-primitives) references of a class depending upon the definition of the array. In the case of primitive data type, the actual value
5 min read
What is the difference between lists and arrays? In programming, lists and arrays are data structures used to organize and store data. Both have their unique features and purposes. Lists are dynamic and flexible, allowing for easy resizing during runtime, while arrays are static with a fixed size. This difference impacts memory usage and performan
8 min read
Difference between an array and a tree Array:An array is a collection of homogeneous(same type) data items stored in contiguous memory locations. For example, if an array is of type âintâ, it can only store integer elements and cannot allow the elements of other types such as double, float, char, etc. The array is a linear data structure
3 min read
Difference between Static and Dynamic Memory Allocation in C In C++, memory allocation is a process by which computer programs and services are assigned physical or virtual memory space. The memory allocation is done either before or at the time of program execution. There are two types of memory allocations: Compile-time or Static Memory AllocationRun-time o
3 min read
Difference Between one-dimensional and two-dimensional array Array is a data structure that is used to store variables that are of similar data types at contiguous locations. The main advantage of the array is random access and cache friendliness. There are mainly three types of the array: One Dimensional (1D) ArrayTwo Dimension (2D) ArrayMultidimensional Arr
3 min read
Is there any difference between int[] a and int a[] in Java? An array in Java is a group of like-typed variables referred to by a common name. Arrays in Java work differently than they do in C/C++. In Java, Array can be declared in the following ways: One-Dimensional Arrays: The general form of a one-dimensional array declaration is type var-name[];ORtype[] v
6 min read
Difference between a Static Queue and a Singly Linked List Static Queue: A queue is an ordered list of elements. It always works in first in first out(FIFO) fashion. All the elements get inserted at the REAR and removed from the FRONT of the queue. In implementation of the static Queue, an array will be used so all operation of queue are index based which m
15+ min read