Dynamic vs. static array in C++

An array is a data structure that stores elements of the same data type in a contiguous memory space.

Example of an integer array
Example of an integer array

Static array

A static array is an array that has a fixed size. This means that once we define the array's size, we cannot change it during the program's execution. The syntax of declaring a static array in C++ is following:

dataType arrayName[arraySize];
Syntax of declaring a static array
  • dataType : The data type of the array elements. It can be int, string or char etc.

  • arrayName : The variable name we give to the array.

  • arraySize : The count of total elements in the array.

For example, to declare an integer array intArray with size 4 we write the following code:

int intArray[4];
Example of integer array

Code example

The following code demonstrates how to use a static integer array in C++.

#include <iostream>
using namespace std;
int main() {
int intArray[4] = {5,6,7,8};
cout << "Array Elements: ";
for (int j = 0; j < 4; j++) {
cout << intArray[j];
cout << " ";
}
return 0;
}

Code explanation

  • Line 1: We include the <iostream> library in our code.

  • Line 2: We use the std namespace, so we don't have to prefix some keywords with std::.

  • Line 6: We define an integer array intArray with size 4. We initialize the array elements at the time of declaration.

  • Lines 8–11: We use a loop to print the array's elements.

Dynamic array

A dynamic array is an array that doesn't have a fixed size, meaning we can change the array's size anytime during the program's execution. We use dynamic memory allocationThe mechanism to manage memory allocation and deallocation at run-time to make our code flexible to changes. to create the dynamic array, so once we have used the memory, we have to deallocateDelete the memory we have created. it to return it to the computer. The syntax of declaring a dynamic array in C++ is the following:

dataType* arrayName = new dataType[arraySize];
Syntax of declaring a dynamic array
  • dataType : The data type of the array elements. It can be int, string or char etc.

  • arrayName : The variable name we give to the array.

  • new : The operator allows memory allocation on the heap.

  • arraySize : The count of total elements in the array.

After we have used the memory and it is no more needed, we deallocate it using the following code:

delete[] arrayName;
Syntax of deallocating a dynamic array

For example, to allocate and deallocate the memory for an integer array intArray with size 4, use the following code.

//Allocating the memory
int* intArray = new int[4];
// performing function of the array
//deallocating the memory
delete[] intArray;
Syntax of allocating and deallocating the memory

Code example

The following code demonstrates how to use a dynamic integer array in C++.

#include <iostream>
using namespace std;
int main() {
int* intArray = new int[4];
intArray[0] = 5;
intArray[1] = 6;
intArray[2] = 7;
intArray[3] = 8;
cout << "Array Elements: ";
for (int j = 0; j < 4; j++) {
cout << intArray[j];
cout << " ";
}
delete[] intArray;
return 0;
}

Code explanation

  • Line 6: We define a dynamic integer array intArray with size 4 using the new operator.

  • Lines 7–10: We define values for the array elements.

  • Lines 12–15: We use a loop to print the array's elements.

  • Line 16: We deallocate the arrays' memory back to the computer.

Comparison

Let's compare the two arrays using this diagram first.

Difference between a static and a dynamic array
Difference between a static and a dynamic array
  • The above diagram shows that a static array A is created on the stack while the dynamic array B has a pointer created on the stack, and that pointer points to the array created at the heap.

Let's summarize the differences.


Static array

Dynamic array

Size

Memory is allocated at compile time, so the array's size cannot be changed when the program is in execution.

Memory is allocated at run time so the array's size can be changed when the program is in execution.

Creation

It is created on stack.

It is created on heap.

Initialization

It can initialize array elements at the time of declaration e.g :

int arr[2] = {1,2);

It cannot initialize array elements at the time of declaration.



Memory Allocation

It uses static memory allocation so the memory is released after the program's execution.

It uses dynamic memory allocation in which memory is not released after the program's execution if we don't deallocate it.

Memory management

It manages memory allocation and deallocation automatically.

We manage memory allocation and deallocation.

Efficiency

It is more efficient due to less overhead and faster allocation time.

It is less efficient due to the overhead of allocating memory at runtime.

Conclusion

We can choose between dynamic and static arrays based on our preferences and code requirements. If we prefer managing memory ourselves and want the array size to be adjustable, we opt for the dynamic arrays. On the other hand, if we want a fixed-size array with automatic memory management, we choose the static arrays.

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved