mercredi 13 juillet 2016

Memory leak with lists c++

The idea of the function is to divide the original list in X Lists all gather in a single array without delete nor creating new Cells. The function do his job great but when I check the leaks with valgrind or Dr. Memory, It appears to have some leak problems...

List* function (List & todivide, int t = 2){
  Cell* aux = todivide.l;       // l is the head of the list
  int tam = (todivide.size()/t == 0) ? todivide.size()/t : todivide.size()/t+1;
  List* arrayoflists = new List [tam];

  for(int i = 0, k = 0; aux != 0; i++){
      if(i%t == 0){
          arrayoflists[k].l = aux;
          aux = aux->sig;
          k++;
      }
      if(i%t == t-1){
          Cell* p = aux->sig;
          aux->sig = 0;
          aux = p;
      }
  }
  l.l = 0;

  return arrayoflists;
}

I see nothing wrong... Any ideas?

Thanks in advance

Aucun commentaire:

Enregistrer un commentaire