Dynamic
Dynamic Arrays in C++ are arrays whose size can be determined during runtime, allowing for greater flexibility compared to static arrays. Dynamic arrays are typically allocated on the heap using pointers and the new keyword. This flexibility comes at the cost of manual memory management, which includes both allocating and deallocating memory.
1. Declaration and Initialization
Dynamic arrays are declared using pointers, and memory is allocated using the new keyword. The size of the array can be specified at runtime.
int* myArray;
int size = 5;
myArray = new int[size]; // Dynamically allocate an array of 5 integers
You can initialize the array after allocation, either manually or using a loop:
for (int i = 0; i < size; i++) {
myArray[i] = i + 1; // Initializes the array elements to 1, 2, 3, 4, 5
}
2. Accessing and Modifying Elements
Elements in a dynamic array are accessed just like in a static array, using their index.
int firstElement = myArray[0]; // Accesses the first element
myArray[2] = 10; // Modifies the third element
3. Resizing a Dynamic Array
Unlike static arrays, dynamic arrays can be resized during runtime. However, resizing a dynamic array requires creating a new array of the desired size, copying the elements from the old array, and then deleting the old array.
int newSize = 10;
int* tempArray = new int[newSize]; // Allocate a new array with the new size
// Copy elements from the old array to the new array
for (int i = 0; i < size; i++) {
tempArray[i] = myArray[i];
}
// Delete the old array
delete[] myArray;
// Point to the new array
myArray = tempArray;
size = newSize; // Update the size variable
4. Deallocating Memory
Since dynamic arrays are allocated on the heap, you must manually deallocate the memory when the array is no longer needed to avoid memory leaks.
delete[] myArray; // Deallocate the memory
myArray = nullptr; // Set the pointer to nullptr to avoid dangling pointers
5. Dynamic Multi-Dimensional Arrays
Dynamic arrays are not limited to one dimension. You can create dynamic multi-dimensional arrays by using arrays of pointers.
Dynamic Two-Dimensional Arrays
int rows = 3, cols = 4;
int** matrix = new int*[rows]; // Allocate an array of int pointers (rows)
for (int i = 0; i < rows; i++) {
matrix[i] = new int[cols]; // Allocate an array of ints (columns) for each row
}
// Accessing and modifying elements
matrix[1][2] = 5;
// Deallocating memory
for (int i = 0; i < rows; i++) {
delete[] matrix[i]; // Delete each row
}
delete[] matrix; // Delete the array of pointers
Dynamic Three-Dimensional Arrays
int layers = 2, rows = 3, cols = 4;
int*** cube = new int**[layers]; // Allocate an array of int** pointers (layers)
for (int i = 0; i < layers; i++) {
cube[i] = new int*[rows]; // Allocate an array of int* pointers (rows) for each layer
for (int j = 0; j < rows; j++) {
cube[i][j] = new int[cols]; // Allocate an array of ints (columns) for each row
}
}
// Accessing and modifying elements
cube[1][2][3] = 10;
// Deallocating memory
for (int i = 0; i < layers; i++) {
for (int j = 0; j < rows; j++) {
delete[] cube[i][j]; // Delete each row
}
delete[] cube[i]; // Delete the array of rows for each layer
}
delete[] cube; // Delete the array of layers
6. Advantages of Dynamic Arrays
- Flexible Size: Dynamic arrays can be resized at runtime, making them more flexible for applications where the number of elements is not known in advance.
- Heap Allocation: Since dynamic arrays are allocated on the heap, they can handle much larger sizes compared to stack-allocated static arrays.
7. Limitations of Dynamic Arrays
- Manual Memory Management: You need to manually manage memory allocation and deallocation, which can be error-prone and lead to memory leaks or dangling pointers if not handled correctly.
- Performance Overhead: Allocating and resizing dynamic arrays can be slower due to heap allocation and the need to copy elements when resizing.
8. Using std::vector for Dynamic Arrays
In C++, the Standard Template Library (STL) provides a dynamic array class called std::vector, which handles memory management automatically. Vectors are generally preferred over manually managed dynamic arrays.
#include <vector>
std::vector<int> myVector = {1, 2, 3, 4, 5};
myVector.push_back(6); // Adds an element at the end
myVector.resize(10); // Resizes the vector to hold 10 elements
// Accessing elements
int element = myVector[2]; // Accesses the third element
// Iterating through the vector
for (int i = 0; i < myVector.size(); i++) {
std::cout << myVector[i] << " ";
}
9. Example:
Here's a complete example demonstrating various operations on a dynamic array:
#include <iostream>
int main() {
int size = 5;
// Dynamically allocate an array of integers
int* myArray = new int[size];
// Initialize the array elements
for (int i = 0; i < size; i++) {
myArray[i] = i + 1;
}
// Print the array elements
std::cout << "Initial array: ";
for (int i = 0; i < size; i++) {
std::cout << myArray[i] << " ";
}
std::cout << std::endl;
// Resize the array to a new size
int newSize = 10;
int* tempArray = new int[newSize];
// Copy elements from the old array to the new array
for (int i = 0; i < size; i++) {
tempArray[i] = myArray[i];
}
// Delete the old array
delete[] myArray;
// Point to the new array and update the size
myArray = tempArray;
size = newSize;
// Initialize the remaining elements of the new array
for (int i = 5; i < size; i++) {
myArray[i] = i + 1;
}
// Print the resized array elements
std::cout << "Resized array: ";
for (int i = 0; i < size; i++) {
std::cout << myArray[i] << " ";
}
std::cout << std::endl;
// Deallocate the memory
delete[] myArray;
return 0;
}
Output:
Initial array: 1 2 3 4 5
Resized array: 1 2 3 4 5 6 7 8 9 10
Initial array: 1 2 3 4 5
Resized array: 1 2 3 4 5 6 7 8 9 10
This example demonstrates how to create, resize, and delete a dynamic array in C++.