为什么我的程序会发生内存泄漏?
Why is there a memory leak in my program?
我实现了一个单链表,其中有一个 SinglyList
class,struct node
,node *head
作为 [=37= 的私有成员];并写了一个析构函数来删除它。我使用 CRT 库使用 _CrtDumpMemoryLeaks()
方法检查内存泄漏。当我调试代码时,在调试控制台中显示发现内存泄漏,这很奇怪,因为我写了一个析构函数来删除它。
这显示在调试控制台中:-
这里是节点class:-
struct node
{
int data = NULL;
node*next = NULL;
};
这是析构函数:-
SinglyList::~SinglyList()
{
node*curr = head,*next;
while(curr != NULL)
{
next = curr->next;
delete curr;
curr = next;
}
}
不明白,怎么会在删除所有节点的时候出现内存泄漏,求助。
编辑:
SinglyList.hpp:-
#pragma once
#include<iostream>
class SinglyList
{
private:
struct node
{
int data = NULL;
node*next = NULL;
};
node*head;
public:
SinglyList();
void Append(const int&);
void Prepend(const int&);
void Print();
void Insert(const int&,const int&);
void Delete(const int&);
void Reverse();
bool isEmpty();
~SinglyList();
};
SinglyList::SinglyList()
{
head = NULL;
}
SinglyList::~SinglyList()
{
node*curr = head,*next;
while(curr != NULL)
{
next = curr->next;
delete curr;
curr = next;
}
}
void SinglyList::Append(const int&data)
{
node*n = new node{data};
if(!head)
{
head = n;
return;
}
node*ptr = head;
while(ptr->next)
ptr = ptr->next;
ptr->next = n;
}
void SinglyList::Prepend(const int&data)
{
node*n = new node{data};
if(!head)
{
head = n;
return;
}
n->next = head;
head = n;
}
void SinglyList::Print()
{
if(!head)
return;
node*ptr = head;
while(ptr)
{
std::cout<<ptr->data<<' ';
ptr = ptr->next;
}
std::cout<<std::endl;
}
void SinglyList::Insert(const int&pos,const int&data)
{
if(pos == 1)
{
Prepend(data);
return;
}
node*n = new node{data};
node*ptr = head;
for(int i = 1;i!=pos-1;i++)
ptr = ptr->next;
n->next = ptr->next;
ptr->next = n;
}
void SinglyList::Delete(const int&pos)
{
if(!head)
return;
node*ptr = head;
if(pos == 1)
{
head= head->next;
delete ptr;
return;
}
for(int i = 1;i!= pos-1;i++)
ptr = ptr->next;
node*temp = ptr->next;
ptr->next = temp->next;
delete temp;
}
void SinglyList::Reverse()
{
if(!head)
return;
node*curr = head,*prev = NULL,*next = NULL;
while(curr)
{
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
head = prev;
}
bool SinglyList::isEmpty()
{
return (!head);
}
main.cpp:-
#include"SinglyList.hpp"
#include<crtdbg.h>
int main()
{
SinglyList nums{};
nums.Append(10);
nums.Append(20);
nums.Append(30);
_CrtDumpMemoryLeaks();
return 0;
}
您在列表被删除之前打印泄漏。试试这个:
#include"SinglyList.hpp"
#include<crtdbg.h>
int main()
{
{
SinglyList nums{};
nums.Append(10);
nums.Append(20);
nums.Append(30);
}
_CrtDumpMemoryLeaks();
return 0;
}
然后,您将在列表被销毁后显示实际泄漏。
我实现了一个单链表,其中有一个 SinglyList
class,struct node
,node *head
作为 [=37= 的私有成员];并写了一个析构函数来删除它。我使用 CRT 库使用 _CrtDumpMemoryLeaks()
方法检查内存泄漏。当我调试代码时,在调试控制台中显示发现内存泄漏,这很奇怪,因为我写了一个析构函数来删除它。
这显示在调试控制台中:-
这里是节点class:-
struct node
{
int data = NULL;
node*next = NULL;
};
这是析构函数:-
SinglyList::~SinglyList()
{
node*curr = head,*next;
while(curr != NULL)
{
next = curr->next;
delete curr;
curr = next;
}
}
不明白,怎么会在删除所有节点的时候出现内存泄漏,求助。
编辑:
SinglyList.hpp:-
#pragma once
#include<iostream>
class SinglyList
{
private:
struct node
{
int data = NULL;
node*next = NULL;
};
node*head;
public:
SinglyList();
void Append(const int&);
void Prepend(const int&);
void Print();
void Insert(const int&,const int&);
void Delete(const int&);
void Reverse();
bool isEmpty();
~SinglyList();
};
SinglyList::SinglyList()
{
head = NULL;
}
SinglyList::~SinglyList()
{
node*curr = head,*next;
while(curr != NULL)
{
next = curr->next;
delete curr;
curr = next;
}
}
void SinglyList::Append(const int&data)
{
node*n = new node{data};
if(!head)
{
head = n;
return;
}
node*ptr = head;
while(ptr->next)
ptr = ptr->next;
ptr->next = n;
}
void SinglyList::Prepend(const int&data)
{
node*n = new node{data};
if(!head)
{
head = n;
return;
}
n->next = head;
head = n;
}
void SinglyList::Print()
{
if(!head)
return;
node*ptr = head;
while(ptr)
{
std::cout<<ptr->data<<' ';
ptr = ptr->next;
}
std::cout<<std::endl;
}
void SinglyList::Insert(const int&pos,const int&data)
{
if(pos == 1)
{
Prepend(data);
return;
}
node*n = new node{data};
node*ptr = head;
for(int i = 1;i!=pos-1;i++)
ptr = ptr->next;
n->next = ptr->next;
ptr->next = n;
}
void SinglyList::Delete(const int&pos)
{
if(!head)
return;
node*ptr = head;
if(pos == 1)
{
head= head->next;
delete ptr;
return;
}
for(int i = 1;i!= pos-1;i++)
ptr = ptr->next;
node*temp = ptr->next;
ptr->next = temp->next;
delete temp;
}
void SinglyList::Reverse()
{
if(!head)
return;
node*curr = head,*prev = NULL,*next = NULL;
while(curr)
{
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
head = prev;
}
bool SinglyList::isEmpty()
{
return (!head);
}
main.cpp:-
#include"SinglyList.hpp"
#include<crtdbg.h>
int main()
{
SinglyList nums{};
nums.Append(10);
nums.Append(20);
nums.Append(30);
_CrtDumpMemoryLeaks();
return 0;
}
您在列表被删除之前打印泄漏。试试这个:
#include"SinglyList.hpp"
#include<crtdbg.h>
int main()
{
{
SinglyList nums{};
nums.Append(10);
nums.Append(20);
nums.Append(30);
}
_CrtDumpMemoryLeaks();
return 0;
}
然后,您将在列表被销毁后显示实际泄漏。