C++ 删除指针时出现分段错误(核心转储)
我试图从链表中删除重复项,但遇到了一个问题,这可能是显而易见的,但我已经很多年没有使用C++ 删除指针时出现分段错误(核心转储),c++,pointers,linked-list,segmentation-fault,duplicate-removal,C++,Pointers,Linked List,Segmentation Fault,Duplicate Removal,我试图从链表中删除重复项,但遇到了一个问题,这可能是显而易见的,但我已经很多年没有使用C++,我无法通过阅读类似的问题来找出我做错了什么 下面是我的部分代码。我删除了不相关的部分(如构造函数、其他方法等) 那么,为什么不允许我删除其他地方新增的内容呢?是因为它是当前范围之外的新的吗 其次,freeing空间很好,但显然不是正确的做法,因为我没有malloc,而是newed 我做错了什么?如何正确杀死已删除的节点 编辑1:根据对我帖子的回复,它更具描述性 Edit2:添加了三种方法的规则如果您使用
C++
,我无法通过阅读类似的问题来找出我做错了什么
下面是我的部分代码。我删除了不相关的部分(如构造函数、其他方法等)
那么,为什么不允许我删除其他地方新增的内容呢?是因为它是当前范围之外的新的吗
其次,free
ing空间很好,但显然不是正确的做法,因为我没有malloc
,而是new
ed
我做错了什么?如何正确杀死已删除的节点
编辑1:根据对我帖子的回复,它更具描述性
Edit2:添加了三种方法的规则如果您使用新的分配内存,则不能使用空闲()
您可以使用malloc()
和free()
或new
和delete
。您也不应该对对象使用malloc()
和free()
,因为它们不像new
和delete那样调用对象构造函数和析构函数
因此,由于您使用new
分配了节点,因此我们可以更改
free(temp); //Here is my problem!!!
到
如果使用new
分配内存,则不能使用free()
您可以使用malloc()
和free()
或new
和delete
。您也不应该对对象使用malloc()
和free()
,因为它们不像new
和delete那样调用对象构造函数和析构函数
因此,由于您使用new
分配了节点,因此我们可以更改
free(temp); //Here is my problem!!!
到
在这种情况下,拥有构造函数和析构函数的代码非常重要
默认情况下,您应该在构造函数中使用“new”,在析构函数中使用“delete”。更具体地说,在构造函数中分配所需的所有资源和在析构函数中取消分配非常重要,这样可以确保没有任何内存泄漏
free(temp);//You don't need this, also you don't need delete.
请发布构造函数和析构函数代码
L.E:
我认为你应该这样做:
template<class T>
class LinkedList
{
private:
struct Node {
T m_data;
Node *m_next;
Node *m_prev;
};
Node* m_first;
Node* m_last;
int m_count;
public:
LinkedList();
~LinkedList();
T GetFirst();
T GetLast();
void AddNode(T node);
void RemoveAt(int index);
T GetAt(int index);
int Count();
};
//constructor
template <typename T>
LinkedList<T>::LinkedList(){
m_count = -1;//-1 == "The list is empty!
}
template <typename T>
void LinkedList<T>::AddNode(T data){
Node* temp = new Node; //new is called only here -> delete is called in destructor or in RemoveAt
temp->m_data = data;
if (m_count > -1){//If the list contains one or more items
temp->m_next = m_first;
temp->m_prev = m_last;
m_last->m_next = temp;
m_last = temp;
m_count++;
}
else if (m_count == -1)
{//If no items are present in the list
m_first = temp;
m_first->m_next = temp;
m_first->m_prev = temp;
m_last = temp;
m_last->m_next = temp;
m_last->m_prev = temp;
m_count = 0;
}
}
template <typename T>
void LinkedList<T>::RemoveAt(int index){
if (index <= m_count)//verify this request is valid
{
Node* temp = m_last;
for (int i = 0; i <= index; ++i){
temp = temp->m_next;
}
temp->m_prev->m_next = temp->m_next;
temp->m_next->m_prev = temp->m_prev;
delete temp;
m_count--;
}
}
template <typename T>
T LinkedList<T>::GetAt(int index)
{
Node* temp = m_first;
if (index <= m_count && index > -1){
int i = 0;
while(i < index){
temp = temp->m_next;
i++;
}
}
return temp->m_data;
}
template <typename T>
T LinkedList<T>::GetFirst() {
return m_first->data;
}
template <typename T>
T LinkedList<T>::GetLast(){
return m_last->data;
}
template <typename T>
int LinkedList<T>::Count(){
return m_count;
}
template <typename T>
LinkedList<T>::~LinkedList(){
while(m_count > -1){
Node* temp = m_first;
m_first = temp->m_next;
delete temp;//delete in destructor
m_count--;
}
}
~Node() { delete next; delete prev; }
模板
类链接列表
{
私人:
结构节点{
T m_数据;
Node*m_next;
节点*m_prev;
};
节点*m_优先;
节点*m_last;
国际货币单位计数;
公众:
LinkedList();
~LinkedList();
T GetFirst();
T GetLast();
void AddNode(T节点);
无效删除(int索引);
T GetAt(int索引);
int Count();
};
//建造师
模板
LinkedList::LinkedList(){
m_count=-1;//-1==“列表为空!
}
模板
void LinkedList::AddNode(T数据){
Node*temp=new Node;//仅在此处调用new->delete在析构函数或RemoveAt中调用
temp->m_数据=数据;
如果(m_count>-1){//如果列表包含一个或多个项目
temp->m_next=m_first;
temp->m_prev=m_last;
m_last->m_next=温度;
m_last=温度;
m_count++;
}
else if(m_count==-1)
{//如果列表中没有项目
m_first=温度;
m_first->m_next=温度;
m_first->m_prev=温度;
m_last=温度;
m_last->m_next=温度;
m_last->m_prev=温度;
m_计数=0;
}
}
模板
void LinkedList::RemoveAt(int索引){
如果(索引m_prev->m_next=temp->m_next;
temp->m_next->m_prev=temp->m_prev;
删除临时文件;
m_count--;
}
}
模板
T LinkedList::GetAt(int索引)
{
节点*temp=m_优先;
如果(索引-1){
int i=0;
而(im_next;
i++;
}
}
返回temp->m_数据;
}
模板
T LinkedList::GetFirst(){
首先返回m_->数据;
}
模板
T LinkedList::GetLast(){
返回m_last->data;
}
模板
int LinkedList::Count(){
返回m_计数;
}
模板
LinkedList::~LinkedList(){
而(m_计数>-1){
节点*temp=m_优先;
m_first=临时->m_next;
delete temp;//在析构函数中删除
m_count--;
}
}
在这种情况下,拥有构造函数和析构函数的代码非常重要
默认情况下,您应该在构造函数中使用“new”,在析构函数中使用“delete”。更具体地说,在构造函数中分配您需要的所有资源并在析构函数中取消分配非常重要,这样可以确保您没有任何内存泄漏
free(temp);//You don't need this, also you don't need delete.
请发布构造函数和析构函数代码
L.E:
我认为你应该这样做:
template<class T>
class LinkedList
{
private:
struct Node {
T m_data;
Node *m_next;
Node *m_prev;
};
Node* m_first;
Node* m_last;
int m_count;
public:
LinkedList();
~LinkedList();
T GetFirst();
T GetLast();
void AddNode(T node);
void RemoveAt(int index);
T GetAt(int index);
int Count();
};
//constructor
template <typename T>
LinkedList<T>::LinkedList(){
m_count = -1;//-1 == "The list is empty!
}
template <typename T>
void LinkedList<T>::AddNode(T data){
Node* temp = new Node; //new is called only here -> delete is called in destructor or in RemoveAt
temp->m_data = data;
if (m_count > -1){//If the list contains one or more items
temp->m_next = m_first;
temp->m_prev = m_last;
m_last->m_next = temp;
m_last = temp;
m_count++;
}
else if (m_count == -1)
{//If no items are present in the list
m_first = temp;
m_first->m_next = temp;
m_first->m_prev = temp;
m_last = temp;
m_last->m_next = temp;
m_last->m_prev = temp;
m_count = 0;
}
}
template <typename T>
void LinkedList<T>::RemoveAt(int index){
if (index <= m_count)//verify this request is valid
{
Node* temp = m_last;
for (int i = 0; i <= index; ++i){
temp = temp->m_next;
}
temp->m_prev->m_next = temp->m_next;
temp->m_next->m_prev = temp->m_prev;
delete temp;
m_count--;
}
}
template <typename T>
T LinkedList<T>::GetAt(int index)
{
Node* temp = m_first;
if (index <= m_count && index > -1){
int i = 0;
while(i < index){
temp = temp->m_next;
i++;
}
}
return temp->m_data;
}
template <typename T>
T LinkedList<T>::GetFirst() {
return m_first->data;
}
template <typename T>
T LinkedList<T>::GetLast(){
return m_last->data;
}
template <typename T>
int LinkedList<T>::Count(){
return m_count;
}
template <typename T>
LinkedList<T>::~LinkedList(){
while(m_count > -1){
Node* temp = m_first;
m_first = temp->m_next;
delete temp;//delete in destructor
m_count--;
}
}
~Node() { delete next; delete prev; }
模板
类链接列表
{
私人:
结构节点{
T m_数据;
Node*m_next;
节点*m_prev;
};
节点*m_优先;
节点*m_last;
国际货币单位计数;
公众:
LinkedList();
~LinkedList();
T GetFirst();
T GetLast();
void AddNode(T节点);
无效删除(int索引);
T GetAt(int索引);
int Count();
};
//建造师
模板
LinkedList::LinkedList(){
m_count=-1;//-1==“列表为空!
}
模板
void LinkedList::AddNode(T数据){
Node*temp=new Node;//仅在此处调用new->delete在析构函数或RemoveAt中调用
temp->m_数据=数据;
如果(m_count>-1){//如果列表包含一个或多个项目
temp->m_next=m_first;
temp->m_prev=m_last;
m_last->m_next=温度;
m_last=温度;
m_count++;
}
else if(m_count==-1)
{//如果列表中没有项目
m_first=温度;
m_first->m_next=温度;
m_first->m_prev=温度;
m_last=温度;
m_last->m_next=温度;
m_last->m_prev=温度;
m_计数=0;
}
}
模板
void LinkedList::RemoveAt(int索引){
如果(索引m_prev->m_next=temp->m_next;
temp->m\u next->m\u prev=temp->m\u p
free(temp); //Here is my problem!!!
delete temp;
~Node() { delete next; delete prev; }