C++编程问题--glibc detected *** ./a.out: munmap_chunk(): invalid pointer: xxxxxx

     编写了一个链表操作的程序,如下:
#include <iostream>

using namespace std;

class Element
{
public:
	Element* prev;
	Element* next;
	int key;
	Element();
	Element(int k);
};
Element::Element()
{
	prev = NULL;
	next = NULL;
	key = 0;
}
Element::Element(int k)
{
	prev = NULL;
	next = NULL;
	key = k;
}

class MyList
{
private:
	Element* head;
public:
	MyList();
	~MyList();
	Element* search(int k);
	void insert(Element* e);
	void delet(Element* e);
	void print();
};
MyList::MyList()
{
	head = NULL;
}
MyList::~MyList()
{
	Element* cur = head;
	Element* prev = head;
	while(cur!=NULL)
	{
		prev = cur;
		cur = cur->next;
		delete prev;
	}
	head = NULL;
}
Element* MyList::search(int k)
{
	Element* temp = head;
	while( (temp!=NULL)&&(temp->key!=k) )
	{
		temp = temp->next;	
	}
	return temp;
}
void MyList::insert(Element* e)
{
	e->next = head;
	if(head!=NULL)
	{
		head->prev = e;
	}
	head = e;
	head->prev = NULL;
}
void MyList::delet(Element* e)
{
	if( e==head )
	{
		head = head->next;
		if(head!=NULL)
		{
			head->prev = NULL;
		}
	}
	else if( e->next==NULL )
	{
		(e->prev)->next = NULL;
	}
	else
	{
		(e->next)->prev = e->prev;
		(e->prev)->next = e->next;
	}
	delete e;
}
void MyList::print()
{
	Element* temp = head;
	while(temp!=NULL)
	{
		cout<<temp->key<<endl;
		temp = temp->next;
	}
}

int main(void)
{
	MyList list;
	Element e1(1),e2(2),e3(3);
	list.insert(&e1);
	list.insert(&e2);
	list.insert(&e3);
	list.print();
	return 0;
}

     编译运行,结果如下:
3
2
1
*** glibc detected *** ./a.out: munmap_chunk(): invalid pointer: 0x00007fff64766020 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x7eb96)[0x7fb14f739b96]
./a.out[0x400988]
./a.out[0x400bcd]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xed)[0x7fb14f6dc76d]
./a.out[0x400819]
======= Memory map: ========
00400000-00401000 r-xp 00000000 08:01 919522                             /home/asus/algorithm/chapter10/a.out
00601000-00602000 r--p 00001000 08:01 919522                             /home/asus/algorithm/chapter10/a.out
00602000-00603000 rw-p 00002000 08:01 919522                             /home/asus/algorithm/chapter10/a.out
01a9d000-01abe000 rw-p 00000000 00:00 0                                  [heap]
7fb14f3bf000-7fb14f4ba000 r-xp 00000000 08:01 1048662                    /lib/x86_64-linux-gnu/libm-2.15.so
7fb14f4ba000-7fb14f6b9000 ---p 000fb000 08:01 1048662                    /lib/x86_64-linux-gnu/libm-2.15.so
7fb14f6b9000-7fb14f6ba000 r--p 000fa000 08:01 1048662                    /lib/x86_64-linux-gnu/libm-2.15.so
7fb14f6ba000-7fb14f6bb000 rw-p 000fb000 08:01 1048662                    /lib/x86_64-linux-gnu/libm-2.15.so
7fb14f6bb000-7fb14f870000 r-xp 00000000 08:01 1048651                    /lib/x86_64-linux-gnu/libc-2.15.so
7fb14f870000-7fb14fa70000 ---p 001b5000 08:01 1048651                    /lib/x86_64-linux-gnu/libc-2.15.so
7fb14fa70000-7fb14fa74000 r--p 001b5000 08:01 1048651                    /lib/x86_64-linux-gnu/libc-2.15.so
7fb14fa74000-7fb14fa76000 rw-p 001b9000 08:01 1048651                    /lib/x86_64-linux-gnu/libc-2.15.so
7fb14fa76000-7fb14fa7b000 rw-p 00000000 00:00 0 
7fb14fa7b000-7fb14fa90000 r-xp 00000000 08:01 1052825                    /lib/x86_64-linux-gnu/libgcc_s.so.1
7fb14fa90000-7fb14fc8f000 ---p 00015000 08:01 1052825                    /lib/x86_64-linux-gnu/libgcc_s.so.1
7fb14fc8f000-7fb14fc90000 r--p 00014000 08:01 1052825                    /lib/x86_64-linux-gnu/libgcc_s.so.1
7fb14fc90000-7fb14fc91000 rw-p 00015000 08:01 1052825                    /lib/x86_64-linux-gnu/libgcc_s.so.1
7fb14fc91000-7fb14fd73000 r-xp 00000000 08:01 1319576                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16
7fb14fd73000-7fb14ff72000 ---p 000e2000 08:01 1319576                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16
7fb14ff72000-7fb14ff7a000 r--p 000e1000 08:01 1319576                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16
7fb14ff7a000-7fb14ff7c000 rw-p 000e9000 08:01 1319576                    /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16
7fb14ff7c000-7fb14ff91000 rw-p 00000000 00:00 0 
7fb14ff91000-7fb14ffb3000 r-xp 00000000 08:01 1048663                    /lib/x86_64-linux-gnu/ld-2.15.so
7fb150198000-7fb15019d000 rw-p 00000000 00:00 0 
7fb1501af000-7fb1501b3000 rw-p 00000000 00:00 0 
7fb1501b3000-7fb1501b4000 r--p 00022000 08:01 1048663                    /lib/x86_64-linux-gnu/ld-2.15.so
7fb1501b4000-7fb1501b6000 rw-p 00023000 08:01 1048663                    /lib/x86_64-linux-gnu/ld-2.15.so
7fff64747000-7fff64768000 rw-p 00000000 00:00 0                          [stack]
7fff647fe000-7fff64800000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
Aborted

     后来发现,问题出在析构函数里, 在那里面要释放并不是动态分配的内存空间:e1,e2,e3。将程序进行如下修改,问题就解决了:
#include <iostream>

using namespace std;

class Element
{
public:
	Element* prev;
	Element* next;
	int key;
	Element();
	Element(int k);
};
Element::Element()
{
	prev = NULL;
	next = NULL;
	key = 0;
}
Element::Element(int k)
{
	prev = NULL;
	next = NULL;
	key = k;
}

class MyList
{
private:
	Element* head;
public:
	MyList();
	~MyList();
	Element* search(int k);
	void insert(int k);
	void delet(Element* e);
	void print();
};
MyList::MyList()
{
	head = NULL;
}
MyList::~MyList()
{
	Element* cur = head;
	Element* prev = head;
	while(cur!=NULL)
	{
		prev = cur;
		cur = cur->next;
		delete prev;
	}
	head = NULL;
}
Element* MyList::search(int k)
{
	Element* temp = head;
	while( (temp!=NULL)&&(temp->key!=k) )
	{
		temp = temp->next;	
	}
	return temp;
}
void MyList::insert(int k)
{
	Element* e = new Element(k);
	e->next = head;
	if(head!=NULL)
	{
		head->prev = e;
	}
	head = e;
	head->prev = NULL;
}
void MyList::delet(Element* e)
{
	if(e==NULL)
	{
		return;
	}
	else if( e==head )
	{
		head = head->next;
		if(head!=NULL)
		{
			head->prev = NULL;
		}
	}
	else if( e->next==NULL )
	{
		(e->prev)->next = NULL;
	}
	else
	{
		(e->next)->prev = e->prev;
		(e->prev)->next = e->next;
	}
	delete e;
}
void MyList::print()
{
	Element* temp = head;
	while(temp!=NULL)
	{
		cout<<temp->key<<endl;
		temp = temp->next;
	}
}

int main(void)
{
	MyList list;

	list.insert(1);
	list.insert(2);
	list.insert(3);

        list.print();      
   
        return 0;
}


你可能感兴趣的:(编程,C++,pointer,glibc,munmap_chunk)