avatar
A weird segmentation fault!# Programming - 葵花宝典
m*e
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;
avatar
m*e
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
avatar
m*e
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++)

avatar
b*n
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.

avatar
m*e
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

avatar
b*n
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)
: {

avatar
m*e
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()

avatar
Q*g
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()

avatar
m*e
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)

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。