C++编程问题--glibc detected *** ./a.out: munmap_chunk(): invalid pointer: xxxxxx
来源:互联网 发布:免费手机小号软件 编辑:程序博客网 时间:2024/05/29 18:42
编写了一个链表操作的程序,如下:
编译运行,结果如下:
后来发现,问题出在析构函数里,在那里面要释放并不是动态分配的内存空间: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(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;}
编译运行,结果如下:
321*** 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.out00601000-00602000 r--p 00001000 08:01 919522 /home/asus/algorithm/chapter10/a.out00602000-00603000 rw-p 00002000 08:01 919522 /home/asus/algorithm/chapter10/a.out01a9d000-01abe000 rw-p 00000000 00:00 0 [heap]7fb14f3bf000-7fb14f4ba000 r-xp 00000000 08:01 1048662 /lib/x86_64-linux-gnu/libm-2.15.so7fb14f4ba000-7fb14f6b9000 ---p 000fb000 08:01 1048662 /lib/x86_64-linux-gnu/libm-2.15.so7fb14f6b9000-7fb14f6ba000 r--p 000fa000 08:01 1048662 /lib/x86_64-linux-gnu/libm-2.15.so7fb14f6ba000-7fb14f6bb000 rw-p 000fb000 08:01 1048662 /lib/x86_64-linux-gnu/libm-2.15.so7fb14f6bb000-7fb14f870000 r-xp 00000000 08:01 1048651 /lib/x86_64-linux-gnu/libc-2.15.so7fb14f870000-7fb14fa70000 ---p 001b5000 08:01 1048651 /lib/x86_64-linux-gnu/libc-2.15.so7fb14fa70000-7fb14fa74000 r--p 001b5000 08:01 1048651 /lib/x86_64-linux-gnu/libc-2.15.so7fb14fa74000-7fb14fa76000 rw-p 001b9000 08:01 1048651 /lib/x86_64-linux-gnu/libc-2.15.so7fb14fa76000-7fb14fa7b000 rw-p 00000000 00:00 0 7fb14fa7b000-7fb14fa90000 r-xp 00000000 08:01 1052825 /lib/x86_64-linux-gnu/libgcc_s.so.17fb14fa90000-7fb14fc8f000 ---p 00015000 08:01 1052825 /lib/x86_64-linux-gnu/libgcc_s.so.17fb14fc8f000-7fb14fc90000 r--p 00014000 08:01 1052825 /lib/x86_64-linux-gnu/libgcc_s.so.17fb14fc90000-7fb14fc91000 rw-p 00015000 08:01 1052825 /lib/x86_64-linux-gnu/libgcc_s.so.17fb14fc91000-7fb14fd73000 r-xp 00000000 08:01 1319576 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.167fb14fd73000-7fb14ff72000 ---p 000e2000 08:01 1319576 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.167fb14ff72000-7fb14ff7a000 r--p 000e1000 08:01 1319576 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.167fb14ff7a000-7fb14ff7c000 rw-p 000e9000 08:01 1319576 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.167fb14ff7c000-7fb14ff91000 rw-p 00000000 00:00 0 7fb14ff91000-7fb14ffb3000 r-xp 00000000 08:01 1048663 /lib/x86_64-linux-gnu/ld-2.15.so7fb150198000-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.so7fb1501b4000-7fb1501b6000 rw-p 00023000 08:01 1048663 /lib/x86_64-linux-gnu/ld-2.15.so7fff64747000-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;}
2 0
- C++编程问题--glibc detected *** ./a.out: munmap_chunk(): invalid pointer: xxxxxx
- linux下C语言编程 一个堆栈错误的原因:*** glibc detected *** ./test: free(): invalid pointer:
- *** glibc detected *** free(): invalid pointer: *** glibc detected *** malloc(): memory corruption:
- munmap_chunk(): invalid pointer
- munmap_chunk(): invalid pointer
- munmap_chunk(): invalid pointer
- 解决munmap_chunk(): invalid pointer
- *** glibc detected *** free(): invalid pointer:解决方法
- “glibc detected”, free(), invalid pointer解决方法
- *** glibc detected *** free(): invalid pointer:解决方法
- *** glibc detected *** free(): invalid pointer:解决方法
- 有关munmap_chunk() invalid pointer错误
- linux C 一个堆栈错误的原因:*** glibc detected *** ./test: free():invalid pointer:
- 关于malloc/free问题---出现*** glibc detected *** free(): invalid pointer的解决方法
- *** glibc detected *** ./test: free(): invalid pointer: 0x0854d1b4 ***
- 全局变量与 *** glibc detected *** ./1: free(): invalid pointer错误
- Linux+QT+OpenCv摄像头调试心得(3)---- glibc detected *** free(): invalid pointer: 0xafeff000
- *** glibc detected *** /root/MAG/bin/MAGServer: free(): invalid pointer: 0xb6b5cb20 ***错误解决办法
- hdu 1242 Rescue 解题报告
- HDU 1226 搜索
- hdu 1829 A Bug's Life
- hdu1002
- 字符设备驱动之led灯的控制实验
- C++编程问题--glibc detected *** ./a.out: munmap_chunk(): invalid pointer: xxxxxx
- POJ 2309 BST 二叉树性质 位运算
- AbstractList.equals(Object o)
- [C\C++基础知识] main函数的参数argc和argv
- action里面获取session以及获取设置session的属性
- C++篇 特破private
- hdu 1503 Advanced Fruits(最长公共子序列变形)
- 题目1214:丑数
- 从Linux内核代码中学习获得结构体成员偏移量的方法