0

Hi I'm coding a C++ program containing a loop consuming too much unnecessary memory, so much that the computer freezes before reaching the end...

Here is how this loop looks like:

float t = 0.20;
while(t<0.35){
    CustomClass a(t);
    a.runCalculations();
    a.writeResultsInFile("results_" + t);
    t += 0.001;
}

If relevant, the program is a physics simulation from which I want results for several values of an external parameter called t for temperature. It seems that the memory excess is due to not "freeing" the space taken by the instance of my class from one execution of the loop to the following, which I thought would be automatic if created without using pointers or the new instruction. I tried doing it with a destructor for the class but it didn't help. Could it be because the main memory use of my class is a 2d array defined with a new instruction in there?

Precision, it seems that the code above is not the problem (thanks for the ones pointing this out) so here is how I initiate my array (by the largest object in my CustomClass) in its constructor:

tab = new int*[h];
for(int i=0; i<h; i++) {
    tab[i] = new int[v];
    for(int j=0; j<v; j++) {
        tab[i][j] = bitd(gen)*2-1; //initializing randomly the lattice
    }
}

bitd(gen) is a random number generator outputing 1 or 0.

And also, another method of my CustomClass object doubles the size of the array in the following way:

int ** temp = new int*[h];
for(int i=0; i<h; i++) {
    temp[i] = new int[v];
    for(int j=0; j<v; j++) {
        temp[i][j] = tab[i/2][j/2];
    }
}
delete[] tab;
tab = temp;

Could there be that I should free the pointer temp?

zakinster
  • 10,508
  • 1
  • 41
  • 52
Liam
  • 593
  • 6
  • 15
  • 4
    There's no way we can respond if you don't show the code of the `CustomClass` class. – Matteo Italia Oct 09 '13 at 13:33
  • I don't see anything wrong with the code you have shown. Probably the problem is in the code that you didn't show, more likely, the constructor and destructor of `CustomClass`. – Cassio Neri Oct 09 '13 at 13:34
  • 4
    For that matter, you should show the actual code of the loop. You never declare `t`, and you add it directly to a char array. – Sneftel Oct 09 '13 at 13:35
  • The answer will probably be that you don't follow one or both of [RAII](http://stackoverflow.com/questions/2321511/what-is-meant-by-resource-acquisition-is-initialization-raii) and [The rule of three](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three). – jrok Oct 09 '13 at 13:37
  • I also suspect `writeResultsInFile` method – Andrey Atapin Oct 09 '13 at 13:37
  • I would also suspect that your `CustomClass` is leaking memory (allocating it somewhere but then not deleting it before it is deleted itself). – Philipp Oct 09 '13 at 13:41
  • Thank you for all for suggesting and asking for precisions. @Andrey I really doubt it is writing results as this methods is just 3 line for writing a single float in a file and closing it. – Liam Oct 09 '13 at 13:46
  • 1
    If the old value of `tab` is not saved anywhere and you replace it by `temp`, then the memory claimed for `tab` is lost and leaked. Also, this is not C++, because if it were, you wouldn't be using raw arrays for data storage. – Daniel Kamil Kozar Oct 09 '13 at 13:50
  • @DanielKamilKozar: so what should I be using to make it "more C++"? – Liam Oct 09 '13 at 13:57
  • @Liam : `std::array` or `std::vector`, in this particular case, of course. – Daniel Kamil Kozar Oct 09 '13 at 14:00
  • @DanielKamilKozar: Thank you I'll have a look at these objects =) – Liam Oct 09 '13 at 14:05

1 Answers1

4

You're leaking memory.

Could there be that I should free te pointer temp?

No. After you allocate the memory for the new array of double size and copy the contents, you should free the memory that tab is pointing to. Right now, you're only deleting the array of pointers with delete [] tab; but the memory that each of those pointers points to is lost. Run a loop and delete each one. Only then do tab = temp.

Better still, use standard containers that handle memory management for you so you can forget messing with raw pointers and focus on your real work instead.

jrok
  • 54,456
  • 9
  • 109
  • 141
  • Thank you, I just added that after the code copying the array and in the class destructor => no more leakage ;) – Liam Oct 09 '13 at 14:10