Can I call a public function in a destructor to free memory?
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
add a comment |
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
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, elsestd::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
add a comment |
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
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
c++ memory linked-list destructor singly-linked-list
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, elsestd::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
add a comment |
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, elsestd::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
add a comment |
1 Answer
1
active
oldest
votes
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 itDeleteTail
), 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.
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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 itDeleteTail
), 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.
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
add a comment |
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 itDeleteTail
), 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.
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
add a comment |
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 itDeleteTail
), 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.
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 itDeleteTail
), 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.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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