由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - A weird segmentation fault!
相关主题
dereference a NULL pointer in C请教boost::any compile错误。
why do we still use dynamic allocation?static variable存在heap还是stack?
弱问c++里有没有NULL这个keyword?C++ Q87: What is wrong with this swap function? (转载)
[合集] What is the inner class wrong[C++ boost::interprocess] 讨论贴
which is faster, table look up or bitwise operator?请教两道linux面试题目
Why should i include .cpp instead of .hC++ Segment Fault
a question about bitwise operation谁知道这个问题的答案 (转载)
C++ Q110: Add without +一个函数指针的问题
相关话题的讨论汇总
话题: intlist话题: list话题: intitem话题: newlist
进入Programming版参与讨论
1 (共1页)
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)

1 (共1页)
进入Programming版参与讨论
相关主题
一个函数指针的问题which is faster, table look up or bitwise operator?
再问一个free()的问题Why should i include .cpp instead of .h
求题目a question about bitwise operation
求题目C++ Q110: Add without +
dereference a NULL pointer in C请教boost::any compile错误。
why do we still use dynamic allocation?static variable存在heap还是stack?
弱问c++里有没有NULL这个keyword?C++ Q87: What is wrong with this swap function? (转载)
[合集] What is the inner class wrong[C++ boost::interprocess] 讨论贴
相关话题的讨论汇总
话题: intlist话题: list话题: intitem话题: newlist