STL Link List Problem

  • Thread starter cplusplusquestion
  • Start date
C

cplusplusquestion

#include <iostream>
#include <list>

using namespace std;

struct Cell{
list<int> vertices;
bool counted;
Cell* nextCell;
Cell(){
counted=false;
nextCell=NULL;
}
~Cell(){
Cell* tmp = nextCell;
Cell* current_ptr = nextCell;
while(tmp != NULL){
current_ptr = current_ptr->nextCell;
delete tmp;
tmp = current_ptr;
}
}
};

int main(){
Cell* a = new Cell;
Cell* b = new Cell;
Cell* c = new Cell;
a->nextCell = b;
b->nextCell = c;
delete a;
return 0;
}

It compiles, but run the program, gets segmentation fault. If there is
no list<int> vertices, it will be fine. Or if no coding in destructor
and keep list<int> vertices, it's no problem too. Any idea?
 
B

Barry

#include <iostream>
#include <list>

using namespace std;

struct Cell{
list<int> vertices;
bool counted;
Cell* nextCell;
Cell(){
counted=false;
nextCell=NULL;
}
~Cell(){
Cell* tmp = nextCell;
Cell* current_ptr = nextCell;
while(tmp != NULL){
current_ptr = current_ptr->nextCell;
delete tmp;

free(tmp);

calling delete will recursively call you ~Cell() and
std::vector said:
tmp = current_ptr;
}
}
};

int main(){
Cell* a = new Cell;
Cell* b = new Cell;
Cell* c = new Cell;
a->nextCell = b;
b->nextCell = c;
delete a;
return 0;
}

It compiles, but run the program, gets segmentation fault. If there is
no list<int> vertices, it will be fine. Or if no coding in destructor
and keep list<int> vertices, it's no problem too. Any idea?

so you're destructing vertices more than once within one Cell

std::vector<int> helping detect the error
 
B

Barry

Barry said:
free(tmp);

calling delete will recursively call you ~Cell() and


so you're destructing vertices more than once within one Cell

std::vector<int> helping detect the error

sorry, list<int>
and moreover, if use *free* other than *delete*, then /vertices/ won't
be destructed, which causes memory leak
 
C

cplusplusquestion

sorry, list<int>
and moreover, if use *free* other than *delete*, then /vertices/ won't
be destructed, which causes memory leak

Thanks! Does that mean I need to malloc() for each object instead of
new?
 
B

Barry

Thanks! Does that mean I need to malloc() for each object instead of
new?

In your case, no, as it works fine

But your code is just some toy, I guess,
It's better to use new/delete malloc/free in pair, so if you need
list<int> or something like that (none POD), then you have to use
new/delete instead of malloc/free.

In a word, redesign. get yourself a C++ book, there must be a LinkedList
in it.
 
K

Kai-Uwe Bux

Thanks! Does that mean I need to malloc() for each object instead of
new?

No, it means you have to fix the code in the destructor. Try:

~Cell(){
delete nextCell;
}


Best

Kai-Uwe Bux
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,250
Latest member
Charlesreero

Latest Threads

Top