#include <iostream>
using namespace std;
int main() {
int size;
cout << "Enter array size: ";
cin >> size;
// Dynamically allocate array
int* arr = new int[size];
cout << "Enter " << size << " elements: ";
for (int i = 0; i < size; i++) {
cin >> arr[i];
}
cout << "\nArray elements: ";
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
// Calculate sum
int sum = 0;
for (int i = 0; i < size; i++) {
sum += arr[i];
}
cout << "Sum: " << sum << endl;
cout << "Average: " << (double)sum / size << endl;
// Find max and min
int max = arr[0], min = arr[0];
for (int i = 1; i < size; i++) {
if (arr[i] > max) max = arr[i];
if (arr[i] < min) min = arr[i];
}
cout << "Maximum: " << max << endl;
cout << "Minimum: " << min << endl;
// Free memory
delete[] arr;
arr = nullptr;
return 0;
}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
This program teaches you how to allocate Dynamic Arrays in C++. Dynamic array allocation allows you to create arrays whose size is determined at runtime, which is essential when the size is not known at compile time. Dynamic arrays are stored on the heap, allowing much larger sizes than stack arrays.
1. What This Program Does
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.
2. Header File Used
#include <iostream>
This header provides:
- cout for displaying output
- cin for taking input from the user
3. 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
4. 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]
5. 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)
6. 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)
7. 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
8. 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
9. 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)
10. return 0;
This ends the program successfully.
Summary
- Dynamic arrays: size determined at runtime, allocated with new[].
- Stored on heap, allowing much larger sizes than stack arrays.
- Access like regular arrays: arr[i] syntax works.
- Always free with delete[] (not delete) to prevent memory leaks.
- Set pointer to nullptr after deletion for safety.
- Understanding dynamic arrays enables flexible, runtime-sized data structures.
- Essential for handling user-determined or variable-sized data.
This program is fundamental for learning dynamic memory allocation, understanding heap vs stack, and preparing for advanced data structures and memory management in C++ programs.