m*********e 发帖数: 37 | 1 I implemented a simple list class. There is a member function "=", which
define the assignment two list.
When I am testing the list class with following code, I got very weird
segmentation fault.
******************************************************
int main()
{
IntList a;
int i;
for(i=0; i<3; i++)
{
a.add_entry(1);
}
IntList b;
for(i=0; i<1; i++)
{
b.add_entry(100);
}
char s;
|
m*********e 发帖数: 37 | 2 Attached is the implementation of the List class:
IntList.h
*****************************************************
#ifndef _INTLIST_H_
#define _INTLIST_H_
#define NULL 0
class IntItem
{
public:
IntItem()
{
//itm = -1;
nxt = NULL;
}
IntItem(int value, IntItem * n = NULL):itm(value), nxt(n){}
int itm;
IntItem *nxt;
};
class IntList
{
public:
IntItem *list;
mutable IntItem *curr;
IntList();
~IntList();
void add |
m*********e 发帖数: 37 | 3 When list b is assigned with list c, three nodes should be allocated for b.
However, it seems the compiler keeps allocating the same memory cell for the
three nodes inside function "concat_list()".
I guess I corrupted the heap somewhere, I just cannot find it out.
【在 m*********e 的大作中提到】 : I implemented a simple list class. There is a member function "=", which : define the assignment two list. : When I am testing the list class with following code, I got very weird : segmentation fault. : ****************************************************** : int main() : { : IntList a; : int i; : for(i=0; i<3; i++)
|
b********n 发帖数: 609 | 4 do a bt at gdb, see what it says
.
the
【在 m*********e 的大作中提到】 : When list b is assigned with list c, three nodes should be allocated for b. : However, it seems the compiler keeps allocating the same memory cell for the : three nodes inside function "concat_list()". : I guess I corrupted the heap somewhere, I just cannot find it out.
|
m*********e 发帖数: 37 | 5 Thanks! I did bt under g++3.2, it said something wrong with concat_list();
I am testing under g++ 4.0.2. The cause it tells now is because in "="
function, I delete the ptr twice.(I don't understand why it happened!)
********************************
void IntList::operator=(IntList newList)
{
IntItem *nptr;
IntItem *ptr = list;
while (ptr)
{
nptr = ptr->nxt;
delete(ptr); //glibc detected here...
ptr = nptr;
}
【在 b********n 的大作中提到】 : do a bt at gdb, see what it says : : . : the
|
b********n 发帖数: 609 | 6 it's obvious, u do delete ptr in operator=, and u delete same
item again in concat_list()
【在 m*********e 的大作中提到】 : Thanks! I did bt under g++3.2, it said something wrong with concat_list(); : I am testing under g++ 4.0.2. The cause it tells now is because in "=" : function, I delete the ptr twice.(I don't understand why it happened!) : ******************************** : void IntList::operator=(IntList newList) : { : IntItem *nptr; : IntItem *ptr = list; : while (ptr) : {
|
m*********e 发帖数: 37 | 7 ??
I delete nothing in concat_list(). There is "//delete newitem;", but it is
commented. Are you talking about this line?
【在 b********n 的大作中提到】 : it's obvious, u do delete ptr in operator=, and u delete same : item again in concat_list()
|
Q**g 发帖数: 183 | 8 change:
IntList.h
void operator=(IntList newList);
==>
void operator=(IntList& newList);
and
IntList.cpp
void IntList::operator=(IntList newList)
==>
void IntList::operator=(IntList& newList)
if you have "void operator=(IntList newList)", there is a copy construction
involved and a temp object is created in a bitwise copying fashion. When the
temp goes out of the scope, its destructor destroys the list which is
shared with your actual parameter.
【在 m*********e 的大作中提到】 : Attached is the implementation of the List class: : IntList.h : ***************************************************** : #ifndef _INTLIST_H_ : #define _INTLIST_H_ : #define NULL 0 : class IntItem : { : public: : IntItem()
|
m*********e 发帖数: 37 | 9 That works :).
Thank you so much!
【在 Q**g 的大作中提到】 : change: : IntList.h : void operator=(IntList newList); : ==> : void operator=(IntList& newList); : and : IntList.cpp : void IntList::operator=(IntList newList) : ==> : void IntList::operator=(IntList& newList)
|