分段故障(内核转储)当我删除指针 [英] Segmentation fault (core dumped) when I delete pointer

查看:239
本文介绍了分段故障(内核转储)当我删除指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从链表中删除重复的,并遇到一个问题,这可能是明显和直接的,但我没有使用 C ++ 多年,我通过阅读SO上的类似问题,我找不到我做错了。

I am trying to remove duplicates from a linked list, and encountered a problem, which is probably obvious and straightforward but I haven't used C++ in many years and I couldn't find out what I am doing wrong by reading similar questions on SO.

下面是我的代码的一部分。我删除了不相关的部分(例如,构造函数,其他方法等)。

Below is parts of my code. I removed irrelevant parts (eg. constructors, other methods, etc).

template<class T>
class Node {
  Node() : data(NULL), next(NULL), prev(NULL) {}
  explicit Node(T d) : data(d), next(NULL), prev(NULL) {}
  explicit Node(T d, Node<T> *nxt, Node<T> *prv) : data(d), next(nxt),    prev(prv) {}
  ~Node() { delete next; delete prev; }

  T data;
  Node *next;
  Node *prev;
};

template<class T>
class LinkedList {
  LinkedList() : head(NULL) {}
  explicit LinkedList(Node<T> *head_node) : head(head_node) {}
  LinkedList& operator=(const LinkedList &copy_list);
  ~LinkedList(); //didn't implement this but I guess delete head is all?

  Node<T>* head;
};


template<class T>
LinkedList<T> RemoveDuplicates(const LinkedList<T> &linked_list) {
  //my = overload creates a whole new list with the same data as the original one
  LinkedList<T> result = linked_list; 

  Node<T> *cur = result.head;
  Node<T> *prev = NULL;

  while (cur) {
    if (...) { //duplicate found  
      Node<T> *temp = cur;
      prev->next = cur->next;
      cur = cur->next;
      cur->prev = prev;
      free(temp); //Here is my problem!!!
    }
    else {
      prev = cur;
      cur = cur->next;
    }
  }
  return result;
}

首先,我做了 delete temp ,我得到分段错误。然后我意识到你只有 delete 。相当不错,但是在中构建整个列表时 new 每个 Node / code>:

So first, I did delete temp and I got Segmentation fault. I then realized you only delete what you new. Fair enough, but I am newing each Node when building the whole list in main:

Node<char> *h = new Node<char>('H'); //I have a constructor to handle this
Node<char> *e = new Node<char>('E');
Node<char> *l1 = new Node<char>('L');
Node<char> *l2 = new Node<char>('L');
Node<char> *o = new Node<char>('O');

h->next = e;
h->prev = NULL;

e->next = l1;
e->prev = h;

//and so on

所以为什么我不允许 delete 在其他地方的东西?是因为 new 在当前范围之外吗?

So why I am not allowed to delete something that was newed somewhere else? Is it because it was newed outside the current scope?

其次, / code>这个空间工作正常,但显然不是正确的事情,因为我没有 malloc ,但 ed!

我做错了什么?如何正确地杀死删除的节点?

What am I doing wrong? How to properly kill that removed node?

Edit1:根据回复我的帖子更具描述性
Edit2:添加3个方法的规则

Made it more descriptive according to replies to my post Rule of 3 methods added

推荐答案

这是其他答案的补充,可以正确识别和解决OP的即时问题。需要快速浏览才能解释接下来会发生什么。

This is an addendum to other answers which correctly identify and solve the OP's immediate problem. A quick walk-though is required to explain what will happen next.

  Node<T> *temp = cur;
  prev->next = cur->next;
  cur = cur->next;
  cur->prev = prev;

此时temp没有被清除,所以next仍然指向两个节点,环绕cur和现在链接在一起。这将导致严重的问题,如果不是:

At this point temp has not been cleared so next and prev still point to the two nodes that used to surround cur and are now linked together. This will cause serious problems if not for:

  free(temp); //Here is my problem!!!

temp由new分配。 NathanOliver的回答已经覆盖了,但潜藏在下面是将会发生什么

free fails because the value pointed to by temp was allocated by new. NathanOliver's answer has this covered, but lurking beneath is what will happen with

delete temp;

这将调用析构函数来清理像一个好的小对象。不幸的是, Node 析构函数如下:

This will invoke the destructor to clean up like a good little object. Unfortunately the Node destructor looks like this:

~Node() { delete next; delete prev; }

temp-> next 到活节点。 temp-> prev 也是如此。

temp->next still points to a live node. So does temp->prev.

下一步和上一步 delete d。它调用它们的析构函数, delete 删除他们触摸的两个节点,并调用一个死亡风暴,将销毁列表中的所有节点。

Next and prev get deleted. Which calls their destructors, deleteing the two nodes they touch, and invokes a deathstorm that will destroy all of the Nodes in the list.

如果双删除不先杀死程序。每个链接节点将尝试 delete 刚刚删除的节点。不好。

If the double-deletes don't kill the program first. Each linked node will try to delete the node that just deleted them. Not good.

奇怪的是, Node 析构函数应该只是照顾自己, c $ c> Node LinkedList 类。

Odds are pretty good that the Node destructor should just look after itself and leave the destruction of the other Nodes to the LinkedList class.

这篇关于分段故障(内核转储)当我删除指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆