Problems with freeing memory

Hello programmers,

After some time I started with c++ again or actually I was always using mostly C and now I am dealing with crushing program and cant find problem.

so I have a structure which I use for linked list:
1
2
3
4
5
6
7
struct rec
{ int words[4];     
  int count;    
  double weight;
  int skips;   
  struct rec * next;
};


and I initiate list by creating first block :
1
2
3
   rec *Records=new rec;
   Records->words[0]=-1;
   Records->next=NULL;


then I am using it a lot and want to free all used memory except first block :
1
2
3
     freeMemory(Records->next);
     Records->next=NULL;
     Records->words[0]=-1;


function for freeing memory is:
1
2
3
4
5
6
7
8
  void freeMemory(rec* records){
    if(records==NULL)
       return;
    if(records->next!=NULL){
      freeMemory(records->next);
    }
    delete records;
  }


But it works only sometimes and on some special places ( always same ) program just crushes.

I am adding block to the list with code:
1
2
3
4
5
6
7
8
9
10
       rec *temp=new rec;
       temp->count=1;
       temp->words[0]=word[0];
       temp->words[1]=word[1];
       temp->words[2]=word[2];
       temp->words[3]=word[3];
       temp->skips=skip;
       temp->weight=0.0;
       temp->next=NULL;
       records->next=temp;


and I perform sorting over the list with swapping:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
  void swap(rec* rec1, rec* rec2){
     rec* temp_unit=new rec;
     temp_unit->count=rec1->count;
     temp_unit->words[0]=rec1->words[0];
     temp_unit->words[1]=rec1->words[1];
     temp_unit->skips=rec1->skips;
     temp_unit->weight=rec1->weight;

     rec1->count=rec2->count;
     rec1->words[0]=rec2->words[0];
     rec1->words[1]=rec2->words[1];
     rec1->skips=rec2->skips;
     rec1->weight=rec2->weight;

     rec2->count=temp_unit->count;
     rec2->words[0]=temp_unit->words[0];
     rec2->words[1]=temp_unit->words[1];
     rec2->skips=temp_unit->skips;
     rec2->weight=temp_unit->weight;

     temp_unit->words[2]=rec1->words[2];
     rec1->words[2]=rec2->words[2];
     rec2->words[2]=temp_unit->words[2];
     
     temp_unit->words[3]=rec1->words[3];
     rec1->words[3]=rec2->words[3];
     rec2->words[3]=temp_unit->words[3];
  
     delete temp_unit;
  }


I cant find the catch, but if I comment freeing memory:
//freeMemory(Records->next);
everything works well but at the end of program I am using sometimes 30-50MB of RAM
Any clue?? THX
Rather than reinventing a linked list, why not use std::list<rec>?
You forgot to make the next NULL after freeing.
1
2
3
4
5
6
7
8
9
  void freeMemory(rec* records){
    if(records==NULL)
       return;
    if(records->next!=NULL){
      freeMemory(records->next);
      records->next = NULL;
    }
    delete records;
  }
But this misses the NULL for the next node you are freeing from.

Personally, I would amend it to return the NULL
1
2
3
4
5
6
7
8
9
  rec* freeMemory(rec* records){
    if (records) {
      if(records->next!=NULL){
        records->next = freeMemory(records->next);
        }
      delete records;
      }
    return NULL;
  }
That makes it easier to free the entire list:
Records = freeMemory( Records );

...or everything but the head:
Records->next = freeMemory( Records->next );

etc.

Hope this helps.
Last edited on
Thanks very much, but it didn't actually helped :( for now I just commented freeing memory. I wouldn't end up with using more than 100MB and program finishes correctly I hope that after that OS frees it automaticly without any request from terminated program. At least freeing during the sorting is working because if I comment off that one I would be using GBs in some seconds :)

btw bubble sort needs 220 mil swaps and quick sort only 6 mil swaps thats real difference :)
Topic archived. No new replies allowed.