首页 > 其他 > 详细

调试死机问题时可根据死机地址快速判断问题出在栈区还是堆区

时间:2017-06-15 16:06:32      阅读:416      评论:0      收藏:0      [点我收藏+]

先看一段会造成死机的代码及其运行结果:

#include <iostream>
using std::cout;
using std::endl;
using std::ostream;

class Tree {
  int height;
public:
  Tree(int treeHeight) : height(treeHeight) {
    cout << __func__ << "(), this = " << this << endl;
  }
  ~Tree() { cout << "~Tree()\n"; }
#if 0 
  friend ostream&
  operator<<(ostream& os, const Tree* t) {
    return os << "Tree height is: "
              << t->height << endl;
  }
#else
  friend ostream&
  operator<<(ostream& os, const Tree& t) {
    return os << "Tree height is: "
              << t.height << endl;
  }
#endif
}; 

int main() {
  cout << "create an object address in stack\n";
  Tree obj(10);

  cout << "create an object address in heap\n";
  Tree* t = new Tree(40);
  delete t;
  //t = nullptr;
  delete t;
}

运行结果:

frank@userver:~/project/test/cpp/new_del$ ./a.out 

create an object address in stack

Tree(), this = 0x7fffdbe5b850

create an object address in heap

Tree(), this = 0x18ab010

~Tree()

~Tree()

*** Error in `./a.out‘: double free or corruption (fasttop): 0x00000000018ab010 ***

Aborted (core dumped)


分析:栈空间的对象地址为0x7fffdbe5b850。堆空间的对象地址为0x18ab010。测试环境为64位虚拟机。栈地址的高4字节地址不为0,堆地址的高4字节通常为0。

本文出自 “用C++写诗” 博客,谢绝转载!

调试死机问题时可根据死机地址快速判断问题出在栈区还是堆区

原文:http://frankniefaquan.blog.51cto.com/12613979/1936961

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!