Hi all.
I have detected something that I can't explain, and hope someone can help me.
I have 2 functions for allocating and de-allocating arrays:
char **c;
void allocate()
{
char **c = new char*[1000];
for(int i = 0; i < 1000; i++)
{
char *g = new char[10000];
for(int j = 0; j < 10000; j++)
{
g[j] = 'k';
}
c[i] = g;
}
}
void deallocate()
{
for(int i = 0; i < 1000; i++)
{
delete [] c[i];
c[i] = NULL;
}
delete c;
c = NULL;
}
void test()
{
cout<<"Start Memory: "<<getMemory()<<endl;
cout<<"Start Time: "<<getTime()<<endl;
allocate();
deallocate();
cout<<"End Memory: "<<getMemory()<<endl;
cout<<"End Time: "<<getTime()<<endl;
}
So this code basically allocates and deallocates an array of char* and measures the time/memory it took.
I've tested this, and got the result that test() either takes very much time and memory (lets say 100) or it takes very few memory/time (lets say 2).
If I run this test a few times right after each other (without closing the program), the results are either very high or very low (randomly).
So my question is, why does the same code sometimes takes very long (with much memory) and sometime very quick (with very low memory usage)?