Memory leak in linked list using dynamic allocation for deleting elements












1














I have been working on a linked list problem, using two pointers to the head and the tail of the list respectively. The issue I have is the following:



When I want to remove from the front or the back of the list, I get memory leaks when the corresponding class methods are implemented using temporary dynamically allocated node pointer and I can't find out what exactly is the problem that is causing the leak.



#include <iostream>

class Node {
public:
Node():data(0),ptrNode(nullptr){};
~Node() {};

// declaring the getters and setters
float getData() const {return data;};
void setData(float d) {data = d;};
Node* getNodePtr() const {return ptrNode;};
void setNodePtr(Node* n){ptrNode = n;};

private:
float data;
Node* ptrNode;
};


class List {

private:
Node * ptrHead;
Node * ptrTail;


public:
List():ptrHead(nullptr),ptrTail(nullptr) {};
~List() {};

void insertAtFront(float x) {
Node * temp = new Node();
temp->setData(x);

if (ptrHead == nullptr) {
ptrHead = temp;
ptrTail = temp;
} else {
temp->setNodePtr(ptrHead);
ptrHead = temp;
}
};

void insertAtBack(float x) {
Node * temp = new Node();
temp->setData(x);

if (ptrHead == nullptr) {
ptrHead = temp;
ptrTail = temp;
} else {
ptrTail->setNodePtr(temp);
ptrTail = temp;
}
};

void removeFromFront() {

if (ptrHead==nullptr) { // if list is empty
std::cout << "List is already empty" << std::endl;
return;
}

if (ptrHead==ptrTail) { // if list has one element
delete ptrHead;
ptrHead = nullptr;
ptrTail = nullptr;
return;
}
// assign current Head to temp, assign next node to head
// delete temp
Node * temp = new Node();
temp = ptrHead;
ptrHead = ptrHead->getNodePtr();
delete temp;
temp = nullptr;
};

void removeFromBack() {

if (ptrHead==nullptr) { // if list is empty
std::cout << "List is already empty" << std::endl;
return;
}

if (ptrHead==ptrTail) { // if list has one element
delete ptrHead;
ptrHead = nullptr;
ptrTail = nullptr;
return;
}

// create two temp Node pointers for this one
Node * sec2last = new Node();
Node * last = new Node();

sec2last = ptrHead;
last = ptrTail;

// locate second to last element and assign it to sec2last
while (sec2last->getNodePtr() != ptrTail) {
sec2last = sec2last->getNodePtr();
}

ptrTail = sec2last;
ptrTail->setNodePtr(nullptr);

delete last;
delete sec2last;
last = nullptr;
sec2last = nullptr;
};

};


I run the following in main():



// global function that dynamically allocates memory in its scope
// for a linked list
void generateList(int x) {
if (x<=0) {
cout << "Give a positive integer!" << endl;
return;
}

List * aList = new List();

for (int i = 0;i<x;i++) {
aList->insertAtFront(i+1);
}

aList->removeFromBack(); // this causes leaks

delete aList;
}

// MAIN
int main() {

for (int i = 0;i<80000000;i++)
generateList(1); // just repeatedly generate dynamic list
// with one element
return 0;
}


I should point out that if we don't use dynamically allocate memory for the temporary nodes within the removeFromFront() and removeFromBack() methods, then the program works fine. But, like I said, It kills me that I can't see why we have leaks with the code above.



Thanks!










share|improve this question


















  • 2




    You're allocating stuff and then immediately discarding the value, like with Node * temp = new Node(); temp = ptrHead.
    – user657267
    Nov 11 at 7:15










  • Node* getNodePtr() const {return ptrNode;}; is problematic (in theory). You couldn't build up a truely const list this way. Typically, you'd solve it by returning a pointer to const (so if first list element is const, next one is const, too) - and to be able to modify the list, you'd provide a non-const overload returning a non-const pointer. However, the node class is intended to be used within list only, so I'd rather make it a private nested class of your list class. You don't need access control then, so you could skip the getters and just make the members public...
    – Aconcagua
    Nov 11 at 7:30










  • ... unless you intend to use the Node class as iterator as well. Then: let node be a public, but still nested class of your list and provide public access to the value; have a public operator++ hopping to next node, but keep the node pointer private - without getters/setters (user shall not modify the list). List class itself then would need to be a friend of node, so it and only it has access to the pointer to modify it.
    – Aconcagua
    Nov 11 at 7:34










  • Thanks for the answers! Also, good point Aconcagua.
    – chlyr314
    Nov 11 at 21:56
















1














I have been working on a linked list problem, using two pointers to the head and the tail of the list respectively. The issue I have is the following:



When I want to remove from the front or the back of the list, I get memory leaks when the corresponding class methods are implemented using temporary dynamically allocated node pointer and I can't find out what exactly is the problem that is causing the leak.



#include <iostream>

class Node {
public:
Node():data(0),ptrNode(nullptr){};
~Node() {};

// declaring the getters and setters
float getData() const {return data;};
void setData(float d) {data = d;};
Node* getNodePtr() const {return ptrNode;};
void setNodePtr(Node* n){ptrNode = n;};

private:
float data;
Node* ptrNode;
};


class List {

private:
Node * ptrHead;
Node * ptrTail;


public:
List():ptrHead(nullptr),ptrTail(nullptr) {};
~List() {};

void insertAtFront(float x) {
Node * temp = new Node();
temp->setData(x);

if (ptrHead == nullptr) {
ptrHead = temp;
ptrTail = temp;
} else {
temp->setNodePtr(ptrHead);
ptrHead = temp;
}
};

void insertAtBack(float x) {
Node * temp = new Node();
temp->setData(x);

if (ptrHead == nullptr) {
ptrHead = temp;
ptrTail = temp;
} else {
ptrTail->setNodePtr(temp);
ptrTail = temp;
}
};

void removeFromFront() {

if (ptrHead==nullptr) { // if list is empty
std::cout << "List is already empty" << std::endl;
return;
}

if (ptrHead==ptrTail) { // if list has one element
delete ptrHead;
ptrHead = nullptr;
ptrTail = nullptr;
return;
}
// assign current Head to temp, assign next node to head
// delete temp
Node * temp = new Node();
temp = ptrHead;
ptrHead = ptrHead->getNodePtr();
delete temp;
temp = nullptr;
};

void removeFromBack() {

if (ptrHead==nullptr) { // if list is empty
std::cout << "List is already empty" << std::endl;
return;
}

if (ptrHead==ptrTail) { // if list has one element
delete ptrHead;
ptrHead = nullptr;
ptrTail = nullptr;
return;
}

// create two temp Node pointers for this one
Node * sec2last = new Node();
Node * last = new Node();

sec2last = ptrHead;
last = ptrTail;

// locate second to last element and assign it to sec2last
while (sec2last->getNodePtr() != ptrTail) {
sec2last = sec2last->getNodePtr();
}

ptrTail = sec2last;
ptrTail->setNodePtr(nullptr);

delete last;
delete sec2last;
last = nullptr;
sec2last = nullptr;
};

};


I run the following in main():



// global function that dynamically allocates memory in its scope
// for a linked list
void generateList(int x) {
if (x<=0) {
cout << "Give a positive integer!" << endl;
return;
}

List * aList = new List();

for (int i = 0;i<x;i++) {
aList->insertAtFront(i+1);
}

aList->removeFromBack(); // this causes leaks

delete aList;
}

// MAIN
int main() {

for (int i = 0;i<80000000;i++)
generateList(1); // just repeatedly generate dynamic list
// with one element
return 0;
}


I should point out that if we don't use dynamically allocate memory for the temporary nodes within the removeFromFront() and removeFromBack() methods, then the program works fine. But, like I said, It kills me that I can't see why we have leaks with the code above.



Thanks!










share|improve this question


















  • 2




    You're allocating stuff and then immediately discarding the value, like with Node * temp = new Node(); temp = ptrHead.
    – user657267
    Nov 11 at 7:15










  • Node* getNodePtr() const {return ptrNode;}; is problematic (in theory). You couldn't build up a truely const list this way. Typically, you'd solve it by returning a pointer to const (so if first list element is const, next one is const, too) - and to be able to modify the list, you'd provide a non-const overload returning a non-const pointer. However, the node class is intended to be used within list only, so I'd rather make it a private nested class of your list class. You don't need access control then, so you could skip the getters and just make the members public...
    – Aconcagua
    Nov 11 at 7:30










  • ... unless you intend to use the Node class as iterator as well. Then: let node be a public, but still nested class of your list and provide public access to the value; have a public operator++ hopping to next node, but keep the node pointer private - without getters/setters (user shall not modify the list). List class itself then would need to be a friend of node, so it and only it has access to the pointer to modify it.
    – Aconcagua
    Nov 11 at 7:34










  • Thanks for the answers! Also, good point Aconcagua.
    – chlyr314
    Nov 11 at 21:56














1












1








1







I have been working on a linked list problem, using two pointers to the head and the tail of the list respectively. The issue I have is the following:



When I want to remove from the front or the back of the list, I get memory leaks when the corresponding class methods are implemented using temporary dynamically allocated node pointer and I can't find out what exactly is the problem that is causing the leak.



#include <iostream>

class Node {
public:
Node():data(0),ptrNode(nullptr){};
~Node() {};

// declaring the getters and setters
float getData() const {return data;};
void setData(float d) {data = d;};
Node* getNodePtr() const {return ptrNode;};
void setNodePtr(Node* n){ptrNode = n;};

private:
float data;
Node* ptrNode;
};


class List {

private:
Node * ptrHead;
Node * ptrTail;


public:
List():ptrHead(nullptr),ptrTail(nullptr) {};
~List() {};

void insertAtFront(float x) {
Node * temp = new Node();
temp->setData(x);

if (ptrHead == nullptr) {
ptrHead = temp;
ptrTail = temp;
} else {
temp->setNodePtr(ptrHead);
ptrHead = temp;
}
};

void insertAtBack(float x) {
Node * temp = new Node();
temp->setData(x);

if (ptrHead == nullptr) {
ptrHead = temp;
ptrTail = temp;
} else {
ptrTail->setNodePtr(temp);
ptrTail = temp;
}
};

void removeFromFront() {

if (ptrHead==nullptr) { // if list is empty
std::cout << "List is already empty" << std::endl;
return;
}

if (ptrHead==ptrTail) { // if list has one element
delete ptrHead;
ptrHead = nullptr;
ptrTail = nullptr;
return;
}
// assign current Head to temp, assign next node to head
// delete temp
Node * temp = new Node();
temp = ptrHead;
ptrHead = ptrHead->getNodePtr();
delete temp;
temp = nullptr;
};

void removeFromBack() {

if (ptrHead==nullptr) { // if list is empty
std::cout << "List is already empty" << std::endl;
return;
}

if (ptrHead==ptrTail) { // if list has one element
delete ptrHead;
ptrHead = nullptr;
ptrTail = nullptr;
return;
}

// create two temp Node pointers for this one
Node * sec2last = new Node();
Node * last = new Node();

sec2last = ptrHead;
last = ptrTail;

// locate second to last element and assign it to sec2last
while (sec2last->getNodePtr() != ptrTail) {
sec2last = sec2last->getNodePtr();
}

ptrTail = sec2last;
ptrTail->setNodePtr(nullptr);

delete last;
delete sec2last;
last = nullptr;
sec2last = nullptr;
};

};


I run the following in main():



// global function that dynamically allocates memory in its scope
// for a linked list
void generateList(int x) {
if (x<=0) {
cout << "Give a positive integer!" << endl;
return;
}

List * aList = new List();

for (int i = 0;i<x;i++) {
aList->insertAtFront(i+1);
}

aList->removeFromBack(); // this causes leaks

delete aList;
}

// MAIN
int main() {

for (int i = 0;i<80000000;i++)
generateList(1); // just repeatedly generate dynamic list
// with one element
return 0;
}


I should point out that if we don't use dynamically allocate memory for the temporary nodes within the removeFromFront() and removeFromBack() methods, then the program works fine. But, like I said, It kills me that I can't see why we have leaks with the code above.



Thanks!










share|improve this question













I have been working on a linked list problem, using two pointers to the head and the tail of the list respectively. The issue I have is the following:



When I want to remove from the front or the back of the list, I get memory leaks when the corresponding class methods are implemented using temporary dynamically allocated node pointer and I can't find out what exactly is the problem that is causing the leak.



#include <iostream>

class Node {
public:
Node():data(0),ptrNode(nullptr){};
~Node() {};

// declaring the getters and setters
float getData() const {return data;};
void setData(float d) {data = d;};
Node* getNodePtr() const {return ptrNode;};
void setNodePtr(Node* n){ptrNode = n;};

private:
float data;
Node* ptrNode;
};


class List {

private:
Node * ptrHead;
Node * ptrTail;


public:
List():ptrHead(nullptr),ptrTail(nullptr) {};
~List() {};

void insertAtFront(float x) {
Node * temp = new Node();
temp->setData(x);

if (ptrHead == nullptr) {
ptrHead = temp;
ptrTail = temp;
} else {
temp->setNodePtr(ptrHead);
ptrHead = temp;
}
};

void insertAtBack(float x) {
Node * temp = new Node();
temp->setData(x);

if (ptrHead == nullptr) {
ptrHead = temp;
ptrTail = temp;
} else {
ptrTail->setNodePtr(temp);
ptrTail = temp;
}
};

void removeFromFront() {

if (ptrHead==nullptr) { // if list is empty
std::cout << "List is already empty" << std::endl;
return;
}

if (ptrHead==ptrTail) { // if list has one element
delete ptrHead;
ptrHead = nullptr;
ptrTail = nullptr;
return;
}
// assign current Head to temp, assign next node to head
// delete temp
Node * temp = new Node();
temp = ptrHead;
ptrHead = ptrHead->getNodePtr();
delete temp;
temp = nullptr;
};

void removeFromBack() {

if (ptrHead==nullptr) { // if list is empty
std::cout << "List is already empty" << std::endl;
return;
}

if (ptrHead==ptrTail) { // if list has one element
delete ptrHead;
ptrHead = nullptr;
ptrTail = nullptr;
return;
}

// create two temp Node pointers for this one
Node * sec2last = new Node();
Node * last = new Node();

sec2last = ptrHead;
last = ptrTail;

// locate second to last element and assign it to sec2last
while (sec2last->getNodePtr() != ptrTail) {
sec2last = sec2last->getNodePtr();
}

ptrTail = sec2last;
ptrTail->setNodePtr(nullptr);

delete last;
delete sec2last;
last = nullptr;
sec2last = nullptr;
};

};


I run the following in main():



// global function that dynamically allocates memory in its scope
// for a linked list
void generateList(int x) {
if (x<=0) {
cout << "Give a positive integer!" << endl;
return;
}

List * aList = new List();

for (int i = 0;i<x;i++) {
aList->insertAtFront(i+1);
}

aList->removeFromBack(); // this causes leaks

delete aList;
}

// MAIN
int main() {

for (int i = 0;i<80000000;i++)
generateList(1); // just repeatedly generate dynamic list
// with one element
return 0;
}


I should point out that if we don't use dynamically allocate memory for the temporary nodes within the removeFromFront() and removeFromBack() methods, then the program works fine. But, like I said, It kills me that I can't see why we have leaks with the code above.



Thanks!







c++ memory-leaks linked-list






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 7:06









chlyr314

92




92








  • 2




    You're allocating stuff and then immediately discarding the value, like with Node * temp = new Node(); temp = ptrHead.
    – user657267
    Nov 11 at 7:15










  • Node* getNodePtr() const {return ptrNode;}; is problematic (in theory). You couldn't build up a truely const list this way. Typically, you'd solve it by returning a pointer to const (so if first list element is const, next one is const, too) - and to be able to modify the list, you'd provide a non-const overload returning a non-const pointer. However, the node class is intended to be used within list only, so I'd rather make it a private nested class of your list class. You don't need access control then, so you could skip the getters and just make the members public...
    – Aconcagua
    Nov 11 at 7:30










  • ... unless you intend to use the Node class as iterator as well. Then: let node be a public, but still nested class of your list and provide public access to the value; have a public operator++ hopping to next node, but keep the node pointer private - without getters/setters (user shall not modify the list). List class itself then would need to be a friend of node, so it and only it has access to the pointer to modify it.
    – Aconcagua
    Nov 11 at 7:34










  • Thanks for the answers! Also, good point Aconcagua.
    – chlyr314
    Nov 11 at 21:56














  • 2




    You're allocating stuff and then immediately discarding the value, like with Node * temp = new Node(); temp = ptrHead.
    – user657267
    Nov 11 at 7:15










  • Node* getNodePtr() const {return ptrNode;}; is problematic (in theory). You couldn't build up a truely const list this way. Typically, you'd solve it by returning a pointer to const (so if first list element is const, next one is const, too) - and to be able to modify the list, you'd provide a non-const overload returning a non-const pointer. However, the node class is intended to be used within list only, so I'd rather make it a private nested class of your list class. You don't need access control then, so you could skip the getters and just make the members public...
    – Aconcagua
    Nov 11 at 7:30










  • ... unless you intend to use the Node class as iterator as well. Then: let node be a public, but still nested class of your list and provide public access to the value; have a public operator++ hopping to next node, but keep the node pointer private - without getters/setters (user shall not modify the list). List class itself then would need to be a friend of node, so it and only it has access to the pointer to modify it.
    – Aconcagua
    Nov 11 at 7:34










  • Thanks for the answers! Also, good point Aconcagua.
    – chlyr314
    Nov 11 at 21:56








2




2




You're allocating stuff and then immediately discarding the value, like with Node * temp = new Node(); temp = ptrHead.
– user657267
Nov 11 at 7:15




You're allocating stuff and then immediately discarding the value, like with Node * temp = new Node(); temp = ptrHead.
– user657267
Nov 11 at 7:15












Node* getNodePtr() const {return ptrNode;}; is problematic (in theory). You couldn't build up a truely const list this way. Typically, you'd solve it by returning a pointer to const (so if first list element is const, next one is const, too) - and to be able to modify the list, you'd provide a non-const overload returning a non-const pointer. However, the node class is intended to be used within list only, so I'd rather make it a private nested class of your list class. You don't need access control then, so you could skip the getters and just make the members public...
– Aconcagua
Nov 11 at 7:30




Node* getNodePtr() const {return ptrNode;}; is problematic (in theory). You couldn't build up a truely const list this way. Typically, you'd solve it by returning a pointer to const (so if first list element is const, next one is const, too) - and to be able to modify the list, you'd provide a non-const overload returning a non-const pointer. However, the node class is intended to be used within list only, so I'd rather make it a private nested class of your list class. You don't need access control then, so you could skip the getters and just make the members public...
– Aconcagua
Nov 11 at 7:30












... unless you intend to use the Node class as iterator as well. Then: let node be a public, but still nested class of your list and provide public access to the value; have a public operator++ hopping to next node, but keep the node pointer private - without getters/setters (user shall not modify the list). List class itself then would need to be a friend of node, so it and only it has access to the pointer to modify it.
– Aconcagua
Nov 11 at 7:34




... unless you intend to use the Node class as iterator as well. Then: let node be a public, but still nested class of your list and provide public access to the value; have a public operator++ hopping to next node, but keep the node pointer private - without getters/setters (user shall not modify the list). List class itself then would need to be a friend of node, so it and only it has access to the pointer to modify it.
– Aconcagua
Nov 11 at 7:34












Thanks for the answers! Also, good point Aconcagua.
– chlyr314
Nov 11 at 21:56




Thanks for the answers! Also, good point Aconcagua.
– chlyr314
Nov 11 at 21:56












1 Answer
1






active

oldest

votes


















5














           Node * temp = new Node();
temp = ptrHead;


This is a memory leak right here. You allocate a new Node and store a pointer to it in temp. Then you overwrite that pointer and leak the node you just allocated.



           // create two temp Node pointers for this one
Node * sec2last = new Node();
Node * last = new Node();

sec2last = ptrHead;
last = ptrTail;


And here you do it again. Why are you allocating new Nodes here?






share|improve this answer





















  • Thanks! I thought this might cause the problem. I guess I was expecting to somehow overcome the leaks by initializing Node data members to null, but I guess even in that case, you still have an allocated address that just contains a Node with null data members. Is there a way to do it though by using dynamic allocation within the List methods? Like I said in my post, I have a working version of it where I just use automatic allocation, which is fine, but I just wondered if this can be done this way without leaks.
    – chlyr314
    Nov 11 at 21:48












  • @chlyr314 Why not just Node* temp = ptrHead; or Node* temp; temp=ptrHead;? Why do you do new Node() when you don't want a new node?
    – David Schwartz
    Nov 12 at 1:12











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246575%2fmemory-leak-in-linked-list-using-dynamic-allocation-for-deleting-elements%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









5














           Node * temp = new Node();
temp = ptrHead;


This is a memory leak right here. You allocate a new Node and store a pointer to it in temp. Then you overwrite that pointer and leak the node you just allocated.



           // create two temp Node pointers for this one
Node * sec2last = new Node();
Node * last = new Node();

sec2last = ptrHead;
last = ptrTail;


And here you do it again. Why are you allocating new Nodes here?






share|improve this answer





















  • Thanks! I thought this might cause the problem. I guess I was expecting to somehow overcome the leaks by initializing Node data members to null, but I guess even in that case, you still have an allocated address that just contains a Node with null data members. Is there a way to do it though by using dynamic allocation within the List methods? Like I said in my post, I have a working version of it where I just use automatic allocation, which is fine, but I just wondered if this can be done this way without leaks.
    – chlyr314
    Nov 11 at 21:48












  • @chlyr314 Why not just Node* temp = ptrHead; or Node* temp; temp=ptrHead;? Why do you do new Node() when you don't want a new node?
    – David Schwartz
    Nov 12 at 1:12
















5














           Node * temp = new Node();
temp = ptrHead;


This is a memory leak right here. You allocate a new Node and store a pointer to it in temp. Then you overwrite that pointer and leak the node you just allocated.



           // create two temp Node pointers for this one
Node * sec2last = new Node();
Node * last = new Node();

sec2last = ptrHead;
last = ptrTail;


And here you do it again. Why are you allocating new Nodes here?






share|improve this answer





















  • Thanks! I thought this might cause the problem. I guess I was expecting to somehow overcome the leaks by initializing Node data members to null, but I guess even in that case, you still have an allocated address that just contains a Node with null data members. Is there a way to do it though by using dynamic allocation within the List methods? Like I said in my post, I have a working version of it where I just use automatic allocation, which is fine, but I just wondered if this can be done this way without leaks.
    – chlyr314
    Nov 11 at 21:48












  • @chlyr314 Why not just Node* temp = ptrHead; or Node* temp; temp=ptrHead;? Why do you do new Node() when you don't want a new node?
    – David Schwartz
    Nov 12 at 1:12














5












5








5






           Node * temp = new Node();
temp = ptrHead;


This is a memory leak right here. You allocate a new Node and store a pointer to it in temp. Then you overwrite that pointer and leak the node you just allocated.



           // create two temp Node pointers for this one
Node * sec2last = new Node();
Node * last = new Node();

sec2last = ptrHead;
last = ptrTail;


And here you do it again. Why are you allocating new Nodes here?






share|improve this answer












           Node * temp = new Node();
temp = ptrHead;


This is a memory leak right here. You allocate a new Node and store a pointer to it in temp. Then you overwrite that pointer and leak the node you just allocated.



           // create two temp Node pointers for this one
Node * sec2last = new Node();
Node * last = new Node();

sec2last = ptrHead;
last = ptrTail;


And here you do it again. Why are you allocating new Nodes here?







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 7:19









David Schwartz

135k14140222




135k14140222












  • Thanks! I thought this might cause the problem. I guess I was expecting to somehow overcome the leaks by initializing Node data members to null, but I guess even in that case, you still have an allocated address that just contains a Node with null data members. Is there a way to do it though by using dynamic allocation within the List methods? Like I said in my post, I have a working version of it where I just use automatic allocation, which is fine, but I just wondered if this can be done this way without leaks.
    – chlyr314
    Nov 11 at 21:48












  • @chlyr314 Why not just Node* temp = ptrHead; or Node* temp; temp=ptrHead;? Why do you do new Node() when you don't want a new node?
    – David Schwartz
    Nov 12 at 1:12


















  • Thanks! I thought this might cause the problem. I guess I was expecting to somehow overcome the leaks by initializing Node data members to null, but I guess even in that case, you still have an allocated address that just contains a Node with null data members. Is there a way to do it though by using dynamic allocation within the List methods? Like I said in my post, I have a working version of it where I just use automatic allocation, which is fine, but I just wondered if this can be done this way without leaks.
    – chlyr314
    Nov 11 at 21:48












  • @chlyr314 Why not just Node* temp = ptrHead; or Node* temp; temp=ptrHead;? Why do you do new Node() when you don't want a new node?
    – David Schwartz
    Nov 12 at 1:12
















Thanks! I thought this might cause the problem. I guess I was expecting to somehow overcome the leaks by initializing Node data members to null, but I guess even in that case, you still have an allocated address that just contains a Node with null data members. Is there a way to do it though by using dynamic allocation within the List methods? Like I said in my post, I have a working version of it where I just use automatic allocation, which is fine, but I just wondered if this can be done this way without leaks.
– chlyr314
Nov 11 at 21:48






Thanks! I thought this might cause the problem. I guess I was expecting to somehow overcome the leaks by initializing Node data members to null, but I guess even in that case, you still have an allocated address that just contains a Node with null data members. Is there a way to do it though by using dynamic allocation within the List methods? Like I said in my post, I have a working version of it where I just use automatic allocation, which is fine, but I just wondered if this can be done this way without leaks.
– chlyr314
Nov 11 at 21:48














@chlyr314 Why not just Node* temp = ptrHead; or Node* temp; temp=ptrHead;? Why do you do new Node() when you don't want a new node?
– David Schwartz
Nov 12 at 1:12




@chlyr314 Why not just Node* temp = ptrHead; or Node* temp; temp=ptrHead;? Why do you do new Node() when you don't want a new node?
– David Schwartz
Nov 12 at 1:12


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246575%2fmemory-leak-in-linked-list-using-dynamic-allocation-for-deleting-elements%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini