Dynamic Array Allocation in C++
Dynamic Array Allocation Program in C++.
BeginnerMemory Management ProgramsExample 8 of 11
dynamic-array-allocation.cpp
1#include <iostream>2using namespace std;34int main() {5 int size;67 cout << "Enter array size: ";8 cin >> size;910 // Dynamically allocate array11 int* arr = new int[size];1213 cout << "Enter " << size << " elements: ";14 for (int i = 0; i < size; i++) {15 cin >> arr[i];16 }1718 cout << "\nArray elements: ";19 for (int i = 0; i < size; i++) {20 cout << arr[i] << " ";21 }22 cout << endl;2324 // Calculate sum25 int sum = 0;26 for (int i = 0; i < size; i++) {27 sum += arr[i];28 }2930 cout << "Sum: " << sum << endl;31 cout << "Average: " << (double)sum / size << endl;3233 // Find max and min34 int max = arr[0], min = arr[0];35 for (int i = 1; i < size; i++) {36 if (arr[i] > max) max = arr[i];37 if (arr[i] < min) min = arr[i];38 }3940 cout << "Maximum: " << max << endl;41 cout << "Minimum: " << min << endl;4243 // Free memory44 delete[] arr;45 arr = nullptr;4647 return 0;48}
Output
Enter array size: 5 Enter 5 elements: 10 20 30 40 50 Array elements: 10 20 30 40 50 Sum: 150 Average: 30 Maximum: 50 Minimum: 10
What's going on
The program demonstrates dynamic array allocation:
Getting array size from user at runtime
Allocating array using new[]
Reading and processing array elements
Performing operations (sum, average, max, min)
Freeing memory using delete[]
Dynamic arrays enable flexible, runtime-determined data structures.
Header File Used
This header provides:
cout for displaying output
cin for taking input from the user
1#include <iostream>
Understanding Dynamic Arrays
Dynamic Array Concept
:
Size determined at runtime
Allocated on heap (not stack)
Can be much larger than stack arrays
Must be explicitly freed
Advantages
:
Size not fixed at compile time
Can handle large arrays
Flexible memory usage
User-determined sizes
Allocating Dynamic Array
Basic Allocation
:
int size;
cin >> size;
int* arr = new int[size];
How it works
:
Size obtained from user at runtime
new[] allocates memory for size elements
Returns pointer to first element
Can access like regular array: arr[0], arr[1]
Reading Array Elements
Input Loop
:
for (int i = 0; i < size; i++) {
cin >> arr[i];
}
How it works
:
Reads elements one by one
Stores in dynamically allocated array
Same syntax as regular arrays
Size can be any value (within memory limits)
Processing Dynamic Array
Operations
:
Sum: add all elements
Average: sum / size
Max/Min: find largest/smallest
Example
:
int sum = 0;
for (int i = 0; i < size; i++) {
sum += arr[i];
}
double average = (double)sum / size;
How it works
:
Process elements like regular arrays
All standard array operations work
Size is known (stored in variable)
Freeing Dynamic Array
Deallocation
:
delete[] arr;
arr = nullptr;
How it works
:
delete[] frees entire array
Returns memory to system
Prevents memory leak
Set to nullptr for safety
Important
:
Always use delete[] (not delete)
Must match new[] with delete[]
Forgetting delete[] causes memory leak
When to Use Dynamic Arrays
Best For
:
Size unknown at compile time
Large arrays (avoid stack overflow)
User-determined sizes
Variable-sized data structures
Runtime configuration
Example Scenarios
:
Reading file data (unknown size)
User input arrays
Large matrices
Dynamic data processing
Important Considerations
Memory Management
:
Always delete[] what you new[]
Memory leak if not freed
Can cause program to run out of memory
Set pointer to nullptr after deletion
Stack vs Heap
:
Stack arrays: fixed size, limited size
Heap arrays: dynamic size, much larger
Stack overflow for large arrays
Heap allows larger allocations
Size Limitations
:
Heap can handle much larger arrays
Limited by available memory
Stack typically limited to few MB
Heap can use GB (system dependent)
return 0;
This ends the program successfully.