473,413 Members | 1,833 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

STL Link List Problem

#include <iostream>
#include <list>

using namespace std;

struct Cell{
list<intvertices;
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<intvertices, it will be fine. Or if no coding in destructor
and keep list<intvertices, it's no problem too. Any idea?

Sep 5 '07 #1
5 1583
cp***************@gmail.com wrote:
#include <iostream>
#include <list>

using namespace std;

struct Cell{
list<intvertices;
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<int>::~vector()
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<intvertices, it will be fine. Or if no coding in destructor
and keep list<intvertices, it's no problem too. Any idea?
so you're destructing vertices more than once within one Cell

std::vector<inthelping detect the error

--
Thanks
Barry
Sep 5 '07 #2
Barry wrote:
cp***************@gmail.com wrote:
>#include <iostream>
#include <list>

using namespace std;

struct Cell{
list<intvertices;
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<int>::~vector()
> 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<intvertices, it will be fine. Or if no coding in destructor
and keep list<intvertices, it's no problem too. Any idea?

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

std::vector<inthelping detect the error
sorry, list<int>
and moreover, if use *free* other than *delete*, then /vertices/ won't
be destructed, which causes memory leak

--
Thanks
Barry
Sep 5 '07 #3
On Sep 5, 9:06 pm, Barry <dhb2...@gmail.comwrote:
Barry wrote:
cplusplusquest...@gmail.com wrote:
#include <iostream>
#include <list>
using namespace std;
struct Cell{
list<intvertices;
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<int>::~vector()
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<intvertices, it will be fine. Or if no coding in destructor
and keep list<intvertices, it's no problem too. Any idea?
so you're destructing vertices more than once within one Cell
std::vector<inthelping detect the error

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

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

Sep 5 '07 #4
cp***************@gmail.com wrote:
>
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<intor 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.
--
Thanks
Barry
Sep 6 '07 #5
cp***************@gmail.com wrote:
On Sep 5, 9:06 pm, Barry <dhb2...@gmail.comwrote:
>Barry wrote:
cplusplusquest...@gmail.com wrote:
#include <iostream>
#include <list>
>using namespace std;
>struct Cell{
list<intvertices;
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<int>::~vector()
> 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<intvertices, it will be fine. Or if no coding in destructor
and keep list<intvertices, it's no problem too. Any idea?
so you're destructing vertices more than once within one Cell
std::vector<inthelping detect the error

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

--
Thanks
Barry

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
Sep 6 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Murat Tasan | last post by:
i'm having trouble with some links using javadoc... basically, here is the example: {@link List}. {@link List#set(int, Object) List.set}. i'll run javadoc, but only the first link shows up....
3
by: VIJAY KUMAR | last post by:
Hi pals, I am using 2 web forms (pages). In first page, i have Datagrid control and on second page i have a hyper link control to the first page and Add value to the data grid/Database. ...
7
by: Shawn Windle | last post by:
----begin node.h-------- #ifndef NODE_H #define NODE_H #include <iostream> //NULL using namespace std; class node {
1
by: Mario | last post by:
I reinstalled IE 6 with all the windows 98 updates and patches and the problem remains exactly. Please tell me how can I solve this ? > Hello, > > Under windows98se and IE 6.0.2800.1106 , few...
8
by: sudhirlko2001 | last post by:
How to swap two nodes of doubly Linklist
14
by: Steve McLellan | last post by:
Hi, Sorry to repost, but this is becoming aggravating, and causing me a lot of wasted time. I've got a reasonably large mixed C++ project, and after a number of builds (but not a constant...
10
by: free2cric | last post by:
Hi, I have a single link list which is sorted. structure of which is like typedef struct mylist { int num; struct mylist *next;
2
by: TheRomance | last post by:
i have a problem about insert integer to link list. sorry it's too long but i try many times, many ways , it's still have an error function is fix . can't change anything about function. i...
3
by: maruf.syfullah | last post by:
Consider the following Class definitions: class AClass { int ai1; int ai2; public: CClass* c; AClass(){}
13
by: Casimir Pohjanraito | last post by:
I have a list of links, with a thumbnail image hidden(resized) next to the link. Complete html&css at end of this post. CSS for the link resizes the image on a:hover. All is good, except the...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.