Loading...
Loading...
Loading Curriculum...
Loading Subject...
Loading Topic...
Loading Lesson...
Loading Lab...
Allows you to request memory during RUNTIME from the system heap. This is essential when you don't know the size of data beforehand.
int* ptr = new int(10);Allocates memory on the heap and returns its address.
delete ptr;Frees the allocated memory back to the system. CRITICAL to avoid leaks.
int n; cin >> n; int* arr = new int[n]; // Array of size n // Freeing dynamic array: delete[] arr;
If you lose the pointer to heap memory before calling delete, that memory stays "taken" until the program exits. This is a memory leak.