I tried to create a LinkedList while learning C++, but a memory leak occurred and I don't know how to resolve it.I've tried many things, but I don't know, so I'd appreciate it if you could let me know.
Note:
Please excuse me for not being able to comment due to lack of credibility.Thank you for your reply.I was wondering if there was a memory leak because the log showed The thread 0x6734 has expired with code-1073741510 (0xc000013a), but if there is a memory leak, it says Detected memory leaks!.It was a complete misunderstanding.Thank you very much。Also look into the Insert implementation.
//LinkedList.cpp:Defines the entry point for the console application.
//
#define_CRTDBG_MAP_ALLOC #include<stdlib.h>#include<crtdbg.h>
# include "stdafx.h"
# include <iostream>
# include <string>
using namespace std;
US>class Node }
int data;
class Node * next;
public:
Node (int param) {
data=param;
next = nullptr;
}
~Node(){
if(next!=nullptr)
next = nullptr;
}
void SetData(int param) {
this ->data=param;
}
int GetData(){
return this ->data;
}
voidSetNext(Node*param){
this ->next=param;
}
Node* GetNext(){
return this ->next;
}
};
classLinkedList {
private:
Node* head = nullptr;
Node* tail = nullptr;
public:
LinkedList(){}
~LinkedList(){
Node* node = head;
while(node!=nullptr){
Node* next=node->GetNext();
delete node;
node = next;
}
}
voidAdd(int value){
if(head==nullptr){
head = new Node (value);
tail = head;
}
else{
Node * next = new Node (value);
tail->SetNext(next);
tail = next;
}
}
US>bool Insert (int value, int index) {
Node* node = head;
if(index==0){
Node* target = new Node (value);
target->SetNext(head);
head = target;
return true;
}
for(inti=0;i<index-1;i++){
if(node==nullptr){
return false;
}
Node* next=node->GetNext();
if(next==nullptr){
break;
}
node = node->GetNext();
}
Node* target = new Node (value);
target->SetNext(node->GetNext());
node->SetNext(target);
return true;
}
void Print() {
Node* node = head;
while(node!=nullptr){
cout<node->GetData()<endl;
node = node->GetNext();
}
}
};
int main()
{
LinkedList*li = new LinkedList();
li ->Add(1);
li ->Add(2);
li ->Add(3);
li ->Insert(0,0);
li ->Insert (7,2);
li ->Print();
delete li;
_CrtDumpMemoryLeaks();
getchar();
return 0;
}
I was wondering if there was a memory leak because the log showed The thread 0x6734 has expired with code-1073741510 (0xc000013a).
in STATUS_CONTROL_C_EXIT{Application Exit by CTRL+C}The application terminated as a result of a CTRL+C.
That's what it means.Do you have any idea?
© 2024 OneMinuteCode. All rights reserved.