I am experimenting with memory allocation and deletion and had a question about how to properly delete/free memory. Below is a very small and working bit of code:
#include <windows.h>
#include <vector>
#include <iostream>
using namespace std;
int main() {
cout << "Initial" << endl;
system("Pause");
double* Array = new double[50000];
for(int i = 0; i < 50000; i++)
{
Array[i] = rand();
}
cout << "Array created" << endl;
system("Pause");
delete[] Array;
cout << "Array deleted" << endl;
system("Pause");
return 1;
}
During each system pause, I used Windows Task Manager to check how much memory my application was using. Below are the numbers from my results:
- Initial 744 KB
- Array Created 1120 KB
- Array Deleted 1124 KB
So have I missed something in my C++ education? Should the memory allocated for the array not be freed after delete[] is called?
See Question&Answers more detail:os