C++ C++;:链表中的潜在内存泄漏
我在写一个链表类,我觉得用于删除特定元素的成员函数可能会导致内存泄漏。代码如下C++ C++;:链表中的潜在内存泄漏,c++,c++11,pointers,memory-leaks,new-operator,C++,C++11,Pointers,Memory Leaks,New Operator,我在写一个链表类,我觉得用于删除特定元素的成员函数可能会导致内存泄漏。代码如下 struct node { int data; node *next; }; class linked_list { private: node *head,*tail; public: linked_list() { head = NULL; tail = NULL; } void add_node(int n) {
struct node
{
int data;
node *next;
};
class linked_list
{
private:
node *head,*tail;
public:
linked_list()
{
head = NULL;
tail = NULL;
}
void add_node(int n)
{
node *tmp = new node;
tmp->data = n;
tmp->next = NULL;
if(head == NULL)
{
head = tmp;
tail = tmp;
}
else
{
tail->next = tmp;
tail = tail->next;
}
}
void DelElem(int locat)
{
int j{1};
node* tmp = new node;
if (locat == 1)
{
tmp = head->next;
head = tmp;
delete tmp;
}
else
{
node* n = head;
while (j < locat - 1)
{
n = n->next;
j++;
}
tmp = n->next;
n->next = tmp->next;
delete tmp;
}
}
struct节点
{
int数据;
节点*下一步;
};
类链表
{
私人:
节点*头,*尾;
公众:
链表()
{
head=NULL;
tail=NULL;
}
void add_节点(int n)
{
node*tmp=新节点;
tmp->data=n;
tmp->next=NULL;
if(head==NULL)
{
水头=tmp;
tail=tmp;
}
其他的
{
tail->next=tmp;
tail=tail->next;
}
}
void DelElem(内部位置)
{
int j{1};
node*tmp=新节点;
如果(locat==1)
{
tmp=头部->下一步;
水头=tmp;
删除tmp;
}
其他的
{
节点*n=头部;
而(jnext;
j++;
}
tmp=n->next;
n->next=tmp->next;
删除tmp;
}
}
对于函数“DelElem”,我首先用新操作符创建了一个指针tmp。但是,我为它分配了不同的地址,这意味着我在初始化时丢失了原来的地址
如何解决此问题?您的代码实例几乎没有问题,我已更正:-
这里,
tmp
最初指向第二个节点,因为head->next
指的是第二个节点。因此,它应该是这样的:-
现在,
tmp
将指向第一个节点,在第二行,head
将指向第二个节点,然后删除由tmp
指向的第一个节点
struct node {
int data;
node* next;
};
class linked_list {
private:
node *head, *tail;
public:
linked_list()
{
head = NULL;
tail = NULL;
}
void add_node(int n)
{
node* tmp = new node;
tmp->data = n;
tmp->next = NULL;
if (head == NULL) {
head = tmp;
tail = tmp;
}
else {
tail->next = tmp;
tail = tail->next;
}
}
void DelElem(int locat)
{
int j{ 1 };
node* tmp;
if (locat == 1) {
tmp = head;
head = head->next;
delete tmp;
}
else {
node* n = head;
while (j < (locat - 1)) {
n = n->next;
j++;
}
tmp = n->next;
n->next = tmp->next;
cout << tmp->data;
delete tmp;
}
}
};
struct节点{
int数据;
节点*下一步;
};
类链表{
私人:
节点*头,*尾;
公众:
链表()
{
head=NULL;
tail=NULL;
}
void add_节点(int n)
{
node*tmp=新节点;
tmp->data=n;
tmp->next=NULL;
if(head==NULL){
水头=tmp;
tail=tmp;
}
否则{
tail->next=tmp;
tail=tail->next;
}
}
void DelElem(内部位置)
{
int j{1};
节点*tmp;
如果(locat==1){
tmp=头部;
头部=头部->下一步;
删除tmp;
}
否则{
节点*n=头部;
而(j<(locat-1)){
n=n->next;
j++;
}
tmp=n->next;
n->next=tmp->next;
cout数据;
删除tmp;
}
}
};
您的代码实例几乎没有问题,我已经纠正了这一点:-这里,
tmp
最初指向第二个节点,因为head->next
指的是第二个节点。因此,它应该是这样的:-
现在,
tmp
将指向第一个节点,在第二行,head
将指向第二个节点,然后删除由tmp
指向的第一个节点
struct node {
int data;
node* next;
};
class linked_list {
private:
node *head, *tail;
public:
linked_list()
{
head = NULL;
tail = NULL;
}
void add_node(int n)
{
node* tmp = new node;
tmp->data = n;
tmp->next = NULL;
if (head == NULL) {
head = tmp;
tail = tmp;
}
else {
tail->next = tmp;
tail = tail->next;
}
}
void DelElem(int locat)
{
int j{ 1 };
node* tmp;
if (locat == 1) {
tmp = head;
head = head->next;
delete tmp;
}
else {
node* n = head;
while (j < (locat - 1)) {
n = n->next;
j++;
}
tmp = n->next;
n->next = tmp->next;
cout << tmp->data;
delete tmp;
}
}
};
struct节点{
int数据;
节点*下一步;
};
类链表{
私人:
节点*头,*尾;
公众:
链表()
{
head=NULL;
tail=NULL;
}
void add_节点(int n)
{
node*tmp=新节点;
tmp->data=n;
tmp->next=NULL;
if(head==NULL){
水头=tmp;
tail=tmp;
}
否则{
tail->next=tmp;
tail=tail->next;
}
}
void DelElem(内部位置)
{
int j{1};
节点*tmp;
如果(locat==1){
tmp=头部;
头部=头部->下一步;
删除tmp;
}
否则{
节点*n=头部;
而(j<(locat-1)){
n=n->next;
j++;
}
tmp=n->next;
n->next=tmp->next;
cout数据;
删除tmp;
}
}
};
为什么你在一个函数中使用new来移除节点?你不应该这样做。我首先用新的操作符创建了一个指针TMP。这是错误的。你不使用new来创建一个指针。你用new来分配一个新的节点。<代码>节点*tMP;< /Cord>创建一个指针。C++不是一个你用new创建每个变量的语言。但是,我分配了一个新的节点。它的地址不同,这意味着我在初始化时丢失了原来的地址。这是正确的,您正在使用分配新节点的不必要的new
创建内存泄漏。“我如何解决此问题?”--首先不要制造问题。你正确地识别了问题,所以要摆脱它。值得思考的是:(这些原则适用于if
语句和循环。)你为什么不使用新的函数来删除节点?你不应该这样做。我首先用新的操作符创建了一个指针TMP。这是错误的。你不使用new来创建一个指针。你用new来分配一个新的节点。<代码>节点*tMP;< /Cord>创建一个指针。C++不是一个你用new创建每个变量的语言。但是,我分配不同。它的ent地址,这意味着我在初始化时丢失了原始地址。这是正确的,您
struct node {
int data;
node* next;
};
class linked_list {
private:
node *head, *tail;
public:
linked_list()
{
head = NULL;
tail = NULL;
}
void add_node(int n)
{
node* tmp = new node;
tmp->data = n;
tmp->next = NULL;
if (head == NULL) {
head = tmp;
tail = tmp;
}
else {
tail->next = tmp;
tail = tail->next;
}
}
void DelElem(int locat)
{
int j{ 1 };
node* tmp;
if (locat == 1) {
tmp = head;
head = head->next;
delete tmp;
}
else {
node* n = head;
while (j < (locat - 1)) {
n = n->next;
j++;
}
tmp = n->next;
n->next = tmp->next;
cout << tmp->data;
delete tmp;
}
}
};