|
-
November 15th, 2016, 04:40 PM
#1
Linked-List: Memory Leaks still occuring.
My code is fully functional if memory leaks are ignored but since I am dealing with dynamic allocation. For every new object created that same object must be destroyed after the task is complete
I am running my script on the GL server via Putty.
I am using the linux command: "valgrind" to check if there are any memory leaks in my code. Unfortunately there are.
Code below:
Code:
#include <iostream>
#include <string>
using namespace std;
//Constants for our for loop
const int START = 0;
const int END = 10;
//Our node structure
struct Node {
int num;
Node * next;
};
//Prototype for our recursive function
void printList(Node* &ptrNode, int direction);
int main (){
//Creating a new pointer to the head of our linked list
Node * head = NULL;
//For loop that builds our linked list with 0-9
for(int i = START; i < END;i++){
if (head == NULL){
Node* newNode = new Node();
head = newNode;
newNode->num = i;
}
else{
Node* newNode= new Node();
newNode->num = i;
newNode->next = head;
head = newNode;
}
}
//Because we don't have a "linked list" we need to pass the head
Node * ptrNode = head;
//First try we are going to go through the linked list starting at 0
cout << "Forward" << endl;
printList(ptrNode, 1);
//Second try we are going to go through the linked list starting at 9
cout << "Backward" << endl;
printList(ptrNode, 0);
//Empties Linked List so there is no memory leak
//PART 2 - Write the while loop to clear out the linked list
//temporary node to store first node
Node *temp = ptrNode;
while(ptrNode != NULL){
//temp equals to ptrNode = head, where head is initially NULL
ptrNode = ptrNode->next; //traverse through the Linked list
delete temp; //delete temp node
temp = NULL; //set the temp node to NULL so we don't lose info
}
return 0;
}
void printList(Node* &ptrNode, int direction){
//PART 1 - Write the code to recursively print the linked list
//Direction indicates forward or backward (0 or 1)
Node *temp;
if(ptrNode->next == NULL){
return; //Exit function
}
else if(direction == 1){
printList(ptrNode->next, direction); //Recursive function to traverse forward
cout << ptrNode->num << endl; //Print next value in the linked list
}
else if(direction == 0){
//Print out the last number in linked list
//first after sorting in descending order
cout << ptrNode->num << endl;
printList(ptrNode->next, direction);
temp = ptrNode->next;
temp->next = ptrNode;
ptrNode->next = NULL;
}
}
-
November 15th, 2016, 04:42 PM
#2
Re: Linked-List: Memory Leaks still occuring.
Forgot to mention, my while loop towards the end of the main() function, is how i should be emptying/deleting the Linked list.
I don't see why it is not traversing through the list.
Any help or insight is appreciated
-
November 15th, 2016, 04:53 PM
#3
Re: Linked-List: Memory Leaks still occuring.
When head is NULL, you are not setting ->next to NULL when you build the list.
Code:
Node *temp = ptrNode;
while(ptrNode != NULL){
ptrNode = ptrNode->next;
delete temp;
temp = NULL;
}
Within the loop, you delete the memory pointed to by temp, but you only set temp once before the loop and not within the loop, so only the memory initially pointed to by ptrNode is deleted. Also note that printList() changes the value of ptrNode - so should be set to head before each call to printList() and before PART 2.
Also note that in c++, to refer to a null ptr then rather than NULL, nullptr is used so
Code:
Node* head = nullptr;
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
November 15th, 2016, 05:05 PM
#4
Re: Linked-List: Memory Leaks still occuring.
Thanks for the insights! I forgot you i still need to keep updating temp value
Yes I fixed it. within my void printList() function.
the line:
Code:
ptrNode->next = NULL;
When the delete method is called that line of code breaks the links in the list causing the while loop to stop after the first node and therefore only freeing one node.
update void function is:
Code:
void printList(Node* &ptrNode, int direction){
//PART 1 - Write the code to recursively print the linked list
//Direction indicates forward or backward (0 or 1)
if(ptrNode->next == NULL){
return; //Exit function
}
else if(direction == 1){
printList(ptrNode->next, direction); //Recursive function to traverse forward
cout << ptrNode->num << endl; //Print next value in the linked list
}
else if(direction == 0){
//Print out the last number in linked list
//first after sorting in descending order
cout << ptrNode->num << endl;
printList(ptrNode->next, direction);
}
}
updated deletion using while loop:
Code:
Node *temp;
while(ptrNode != NULL){
//temp equals to ptrNode = head, where head is initially NULL
temp = ptrNode;
ptrNode = ptrNode->next; //traverse through the Linked list
delete temp; //delete temp node
temp = NULL; //set the temp node to NULL so we don't lose info
}
Memory Leak free code now!
-
November 15th, 2016, 05:47 PM
#5
Re: Linked-List: Memory Leaks still occuring.
You don't actually need to set temp to NULL after delete as either the while loop will terminate or temp will be set again.
for printList(), you are passing ptrNode * by reference, so will be changed. Do you need to pass by reference or just pass by value as you are passing a pointer?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|