An array is a data structure that stores elements of the same data type in a contiguous memory space.
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];
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];
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;}
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.
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
dataType* arrayName = new dataType[arraySize];
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;
For example, to allocate and deallocate the memory for an integer array intArray
with size 4,
use the following code.
//Allocating the memoryint* intArray = new int[4];// performing function of the array//deallocating the memorydelete[] intArray;
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;}
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.
Let's compare the two arrays using this diagram first.
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. |
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