Can I call a public function in a destructor to free memory?












0















I am implementing a LinkedList. Instead of redoing the work, can I reuse a public function that I wrote to delete and free the nodes(memory too) in the destructor. Output turn out to be fine/correct, but I'm wondering if I'm missing anything behind the scene. I've seen this but my main concern is about whether am I doing it correctly in terms of freeing up the memory in my C++ code.



My destructor:



~LinkedList(){
Node *next = head;
while(head != NULL){
DeleteEndVal();
}
}


My public function DeleteEndVal();:



// delete value from the end of the list
int DeleteEndVal(){
if(CheckListEmpty() == true){
cout << "Empty list. Nothing to delete." << endl;
return -1;
}
else{
int val;
Node *prev;
Node *cur;
if(head->next == NULL){
val = tail->data;
head = NULL;
tail = NULL;

}
else{
prev = head;
cur = head->next;
while (cur->next != NULL){
prev = prev->next;
cur = cur->next;
val = cur->data;
}
prev->next = NULL;
free(cur);
}
return val;
}
}









share|improve this question























  • can I reuse a public function that I wrote to delete and free the nodes(memory too) in the destructor -- If the function works, yes you can. But if you're going to do things this way, why not delete the first value repeatedly instead of deleting the last value? Deleting the last value incurs a performance penalty due to you having to traverse all the way to the end.

    – PaulMcKenzie
    Nov 25 '18 at 22:40













  • @PaulMcKenzie Ah! You are right. Thanks for bringing that up!

    – ccsalison
    Nov 25 '18 at 23:31











  • The other thing to watch out for is that the function you're calling, it shouldn't throw an exception if you're going to utilize it in the destructor, or at the very least, make sure no exception escapes from the destructor. Thus your destructor needs to make sure the exception doesn't escape from it, else std::terminate will be called. You can ensure this in many ways, but just to round out the things you have to watch out for when calling other functions in a destructor.

    – PaulMcKenzie
    Nov 26 '18 at 1:10


















0















I am implementing a LinkedList. Instead of redoing the work, can I reuse a public function that I wrote to delete and free the nodes(memory too) in the destructor. Output turn out to be fine/correct, but I'm wondering if I'm missing anything behind the scene. I've seen this but my main concern is about whether am I doing it correctly in terms of freeing up the memory in my C++ code.



My destructor:



~LinkedList(){
Node *next = head;
while(head != NULL){
DeleteEndVal();
}
}


My public function DeleteEndVal();:



// delete value from the end of the list
int DeleteEndVal(){
if(CheckListEmpty() == true){
cout << "Empty list. Nothing to delete." << endl;
return -1;
}
else{
int val;
Node *prev;
Node *cur;
if(head->next == NULL){
val = tail->data;
head = NULL;
tail = NULL;

}
else{
prev = head;
cur = head->next;
while (cur->next != NULL){
prev = prev->next;
cur = cur->next;
val = cur->data;
}
prev->next = NULL;
free(cur);
}
return val;
}
}









share|improve this question























  • can I reuse a public function that I wrote to delete and free the nodes(memory too) in the destructor -- If the function works, yes you can. But if you're going to do things this way, why not delete the first value repeatedly instead of deleting the last value? Deleting the last value incurs a performance penalty due to you having to traverse all the way to the end.

    – PaulMcKenzie
    Nov 25 '18 at 22:40













  • @PaulMcKenzie Ah! You are right. Thanks for bringing that up!

    – ccsalison
    Nov 25 '18 at 23:31











  • The other thing to watch out for is that the function you're calling, it shouldn't throw an exception if you're going to utilize it in the destructor, or at the very least, make sure no exception escapes from the destructor. Thus your destructor needs to make sure the exception doesn't escape from it, else std::terminate will be called. You can ensure this in many ways, but just to round out the things you have to watch out for when calling other functions in a destructor.

    – PaulMcKenzie
    Nov 26 '18 at 1:10
















0












0








0








I am implementing a LinkedList. Instead of redoing the work, can I reuse a public function that I wrote to delete and free the nodes(memory too) in the destructor. Output turn out to be fine/correct, but I'm wondering if I'm missing anything behind the scene. I've seen this but my main concern is about whether am I doing it correctly in terms of freeing up the memory in my C++ code.



My destructor:



~LinkedList(){
Node *next = head;
while(head != NULL){
DeleteEndVal();
}
}


My public function DeleteEndVal();:



// delete value from the end of the list
int DeleteEndVal(){
if(CheckListEmpty() == true){
cout << "Empty list. Nothing to delete." << endl;
return -1;
}
else{
int val;
Node *prev;
Node *cur;
if(head->next == NULL){
val = tail->data;
head = NULL;
tail = NULL;

}
else{
prev = head;
cur = head->next;
while (cur->next != NULL){
prev = prev->next;
cur = cur->next;
val = cur->data;
}
prev->next = NULL;
free(cur);
}
return val;
}
}









share|improve this question














I am implementing a LinkedList. Instead of redoing the work, can I reuse a public function that I wrote to delete and free the nodes(memory too) in the destructor. Output turn out to be fine/correct, but I'm wondering if I'm missing anything behind the scene. I've seen this but my main concern is about whether am I doing it correctly in terms of freeing up the memory in my C++ code.



My destructor:



~LinkedList(){
Node *next = head;
while(head != NULL){
DeleteEndVal();
}
}


My public function DeleteEndVal();:



// delete value from the end of the list
int DeleteEndVal(){
if(CheckListEmpty() == true){
cout << "Empty list. Nothing to delete." << endl;
return -1;
}
else{
int val;
Node *prev;
Node *cur;
if(head->next == NULL){
val = tail->data;
head = NULL;
tail = NULL;

}
else{
prev = head;
cur = head->next;
while (cur->next != NULL){
prev = prev->next;
cur = cur->next;
val = cur->data;
}
prev->next = NULL;
free(cur);
}
return val;
}
}






c++ memory linked-list destructor singly-linked-list






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 25 '18 at 22:35









ccsalisonccsalison

2417




2417













  • can I reuse a public function that I wrote to delete and free the nodes(memory too) in the destructor -- If the function works, yes you can. But if you're going to do things this way, why not delete the first value repeatedly instead of deleting the last value? Deleting the last value incurs a performance penalty due to you having to traverse all the way to the end.

    – PaulMcKenzie
    Nov 25 '18 at 22:40













  • @PaulMcKenzie Ah! You are right. Thanks for bringing that up!

    – ccsalison
    Nov 25 '18 at 23:31











  • The other thing to watch out for is that the function you're calling, it shouldn't throw an exception if you're going to utilize it in the destructor, or at the very least, make sure no exception escapes from the destructor. Thus your destructor needs to make sure the exception doesn't escape from it, else std::terminate will be called. You can ensure this in many ways, but just to round out the things you have to watch out for when calling other functions in a destructor.

    – PaulMcKenzie
    Nov 26 '18 at 1:10





















  • can I reuse a public function that I wrote to delete and free the nodes(memory too) in the destructor -- If the function works, yes you can. But if you're going to do things this way, why not delete the first value repeatedly instead of deleting the last value? Deleting the last value incurs a performance penalty due to you having to traverse all the way to the end.

    – PaulMcKenzie
    Nov 25 '18 at 22:40













  • @PaulMcKenzie Ah! You are right. Thanks for bringing that up!

    – ccsalison
    Nov 25 '18 at 23:31











  • The other thing to watch out for is that the function you're calling, it shouldn't throw an exception if you're going to utilize it in the destructor, or at the very least, make sure no exception escapes from the destructor. Thus your destructor needs to make sure the exception doesn't escape from it, else std::terminate will be called. You can ensure this in many ways, but just to round out the things you have to watch out for when calling other functions in a destructor.

    – PaulMcKenzie
    Nov 26 '18 at 1:10



















can I reuse a public function that I wrote to delete and free the nodes(memory too) in the destructor -- If the function works, yes you can. But if you're going to do things this way, why not delete the first value repeatedly instead of deleting the last value? Deleting the last value incurs a performance penalty due to you having to traverse all the way to the end.

– PaulMcKenzie
Nov 25 '18 at 22:40







can I reuse a public function that I wrote to delete and free the nodes(memory too) in the destructor -- If the function works, yes you can. But if you're going to do things this way, why not delete the first value repeatedly instead of deleting the last value? Deleting the last value incurs a performance penalty due to you having to traverse all the way to the end.

– PaulMcKenzie
Nov 25 '18 at 22:40















@PaulMcKenzie Ah! You are right. Thanks for bringing that up!

– ccsalison
Nov 25 '18 at 23:31





@PaulMcKenzie Ah! You are right. Thanks for bringing that up!

– ccsalison
Nov 25 '18 at 23:31













The other thing to watch out for is that the function you're calling, it shouldn't throw an exception if you're going to utilize it in the destructor, or at the very least, make sure no exception escapes from the destructor. Thus your destructor needs to make sure the exception doesn't escape from it, else std::terminate will be called. You can ensure this in many ways, but just to round out the things you have to watch out for when calling other functions in a destructor.

– PaulMcKenzie
Nov 26 '18 at 1:10







The other thing to watch out for is that the function you're calling, it shouldn't throw an exception if you're going to utilize it in the destructor, or at the very least, make sure no exception escapes from the destructor. Thus your destructor needs to make sure the exception doesn't escape from it, else std::terminate will be called. You can ensure this in many ways, but just to round out the things you have to watch out for when calling other functions in a destructor.

– PaulMcKenzie
Nov 26 '18 at 1:10














1 Answer
1






active

oldest

votes


















2














You can call a function inside your destructor, so first-off answering your original question: Yes, that's ok.



There are several other aspects though:



First, you should not recode a basic concept like a linked list by hand. This is error-prone and/or may be an inefficient implementation. Use foundation classes or standard C++ libraries instead.



When sticking to your code, I wonder why:




  • there is a Node *next = head; in the destructor. IMHO, it does nothing.

  • you seem to have a tail attribute. If the method is removing the tail element (maybe rename it DeleteTail), then you shouldn't walk through the entire linked list. Instead, take the tail, get its previous element (I assume your linked list is bi-directional), and update that link instead.

  • as a consequence, your current implementation has a nested while loop to destroy the list, taking an order of n*n in time. For the destruction use-case, you don't even need to update the elements, use a simple while loop and destroy them without relinking.






share|improve this answer
























  • 1) I deleted the Node *next = head; and it seems to not have any affect on the output. 2) My linked list is singly. I think I would remove it with head as per suggestion by Paul above. 3) You are right. Let me see how I can rewrite and optimize it. Thanks Thomas!

    – ccsalison
    Nov 25 '18 at 23:32













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%2f53472700%2fcan-i-call-a-public-function-in-a-destructor-to-free-memory%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









2














You can call a function inside your destructor, so first-off answering your original question: Yes, that's ok.



There are several other aspects though:



First, you should not recode a basic concept like a linked list by hand. This is error-prone and/or may be an inefficient implementation. Use foundation classes or standard C++ libraries instead.



When sticking to your code, I wonder why:




  • there is a Node *next = head; in the destructor. IMHO, it does nothing.

  • you seem to have a tail attribute. If the method is removing the tail element (maybe rename it DeleteTail), then you shouldn't walk through the entire linked list. Instead, take the tail, get its previous element (I assume your linked list is bi-directional), and update that link instead.

  • as a consequence, your current implementation has a nested while loop to destroy the list, taking an order of n*n in time. For the destruction use-case, you don't even need to update the elements, use a simple while loop and destroy them without relinking.






share|improve this answer
























  • 1) I deleted the Node *next = head; and it seems to not have any affect on the output. 2) My linked list is singly. I think I would remove it with head as per suggestion by Paul above. 3) You are right. Let me see how I can rewrite and optimize it. Thanks Thomas!

    – ccsalison
    Nov 25 '18 at 23:32


















2














You can call a function inside your destructor, so first-off answering your original question: Yes, that's ok.



There are several other aspects though:



First, you should not recode a basic concept like a linked list by hand. This is error-prone and/or may be an inefficient implementation. Use foundation classes or standard C++ libraries instead.



When sticking to your code, I wonder why:




  • there is a Node *next = head; in the destructor. IMHO, it does nothing.

  • you seem to have a tail attribute. If the method is removing the tail element (maybe rename it DeleteTail), then you shouldn't walk through the entire linked list. Instead, take the tail, get its previous element (I assume your linked list is bi-directional), and update that link instead.

  • as a consequence, your current implementation has a nested while loop to destroy the list, taking an order of n*n in time. For the destruction use-case, you don't even need to update the elements, use a simple while loop and destroy them without relinking.






share|improve this answer
























  • 1) I deleted the Node *next = head; and it seems to not have any affect on the output. 2) My linked list is singly. I think I would remove it with head as per suggestion by Paul above. 3) You are right. Let me see how I can rewrite and optimize it. Thanks Thomas!

    – ccsalison
    Nov 25 '18 at 23:32
















2












2








2







You can call a function inside your destructor, so first-off answering your original question: Yes, that's ok.



There are several other aspects though:



First, you should not recode a basic concept like a linked list by hand. This is error-prone and/or may be an inefficient implementation. Use foundation classes or standard C++ libraries instead.



When sticking to your code, I wonder why:




  • there is a Node *next = head; in the destructor. IMHO, it does nothing.

  • you seem to have a tail attribute. If the method is removing the tail element (maybe rename it DeleteTail), then you shouldn't walk through the entire linked list. Instead, take the tail, get its previous element (I assume your linked list is bi-directional), and update that link instead.

  • as a consequence, your current implementation has a nested while loop to destroy the list, taking an order of n*n in time. For the destruction use-case, you don't even need to update the elements, use a simple while loop and destroy them without relinking.






share|improve this answer













You can call a function inside your destructor, so first-off answering your original question: Yes, that's ok.



There are several other aspects though:



First, you should not recode a basic concept like a linked list by hand. This is error-prone and/or may be an inefficient implementation. Use foundation classes or standard C++ libraries instead.



When sticking to your code, I wonder why:




  • there is a Node *next = head; in the destructor. IMHO, it does nothing.

  • you seem to have a tail attribute. If the method is removing the tail element (maybe rename it DeleteTail), then you shouldn't walk through the entire linked list. Instead, take the tail, get its previous element (I assume your linked list is bi-directional), and update that link instead.

  • as a consequence, your current implementation has a nested while loop to destroy the list, taking an order of n*n in time. For the destruction use-case, you don't even need to update the elements, use a simple while loop and destroy them without relinking.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 25 '18 at 22:46









Thomas JacobThomas Jacob

31118




31118













  • 1) I deleted the Node *next = head; and it seems to not have any affect on the output. 2) My linked list is singly. I think I would remove it with head as per suggestion by Paul above. 3) You are right. Let me see how I can rewrite and optimize it. Thanks Thomas!

    – ccsalison
    Nov 25 '18 at 23:32





















  • 1) I deleted the Node *next = head; and it seems to not have any affect on the output. 2) My linked list is singly. I think I would remove it with head as per suggestion by Paul above. 3) You are right. Let me see how I can rewrite and optimize it. Thanks Thomas!

    – ccsalison
    Nov 25 '18 at 23:32



















1) I deleted the Node *next = head; and it seems to not have any affect on the output. 2) My linked list is singly. I think I would remove it with head as per suggestion by Paul above. 3) You are right. Let me see how I can rewrite and optimize it. Thanks Thomas!

– ccsalison
Nov 25 '18 at 23:32







1) I deleted the Node *next = head; and it seems to not have any affect on the output. 2) My linked list is singly. I think I would remove it with head as per suggestion by Paul above. 3) You are right. Let me see how I can rewrite and optimize it. Thanks Thomas!

– ccsalison
Nov 25 '18 at 23:32






















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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53472700%2fcan-i-call-a-public-function-in-a-destructor-to-free-memory%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







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

Refactoring coordinates for Minecraft Pi buildings written in Python