Deallocating from C++ priority_queue Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!What are the differences between a pointer variable and a reference variable in C++?How can I profile C++ code running on Linux?The Definitive C++ Book Guide and ListWhat is the effect of extern “C” in C++?What is the “-->” operator in C++?Why do we need virtual functions in C++?Easiest way to convert int to string in C++C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?Why should C++ programmers minimize use of 'new'?Why is reading lines from stdin much slower in C++ than Python?
What is best way to wire a ceiling receptacle in this situation?
Semigroups with no morphisms between them
How do living politicians protect their readily obtainable signatures from misuse?
Why are vacuum tubes still used in amateur radios?
Why does it sometimes sound good to play a grace note as a lead in to a note in a melody?
Is multiple magic items in one inherently imbalanced?
Dynamic filling of a region of a polar plot
Amount of permutations on an NxNxN Rubik's Cube
An adverb for when you're not exaggerating
Crossing US/Canada Border for less than 24 hours
How does Belgium enforce obligatory attendance in elections?
What's the difference between the capability remove_users and delete_users?
I can't produce songs
Can the Flaming Sphere spell be rammed into multiple Tiny creatures that are in the same 5-foot square?
In musical terms, what properties are varied by the human voice to produce different words / syllables?
macOS: Name for app shortcut screen found by pinching with thumb and three fingers
Do wooden building fires get hotter than 600°C?
Did any compiler fully use 80-bit floating point?
What to do with repeated rejections for phd position
Creating an enum from its name not value
What's the point of the test set?
The test team as an enemy of development? And how can this be avoided?
Strange behavior of Object.defineProperty() in JavaScript
What would you call this weird metallic apparatus that allows you to lift people?
Deallocating from C++ priority_queue
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!What are the differences between a pointer variable and a reference variable in C++?How can I profile C++ code running on Linux?The Definitive C++ Book Guide and ListWhat is the effect of extern “C” in C++?What is the “-->” operator in C++?Why do we need virtual functions in C++?Easiest way to convert int to string in C++C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?Why should C++ programmers minimize use of 'new'?Why is reading lines from stdin much slower in C++ than Python?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
Is it safe to delete an element in a std::priority_queue
and pop it afterwards or does the pop try to do anything with the reference other than discarding it?
while(!priority_queue.empty())
delete priority_queue.top();
priority_queue.pop();
c++ memory-management
|
show 4 more comments
Is it safe to delete an element in a std::priority_queue
and pop it afterwards or does the pop try to do anything with the reference other than discarding it?
while(!priority_queue.empty())
delete priority_queue.top();
priority_queue.pop();
c++ memory-management
3
What are you storing in your priority queue?
– Konrad Rudolph
Mar 22 at 11:36
The priority queue contains std::pairs<int, double>. I'm trying to fix a memory leak in somebody's code.
– John
Mar 22 at 11:38
2
How are you even able to calldelete
then if you're not storing pointers?
– Sombrero Chicken
Mar 22 at 11:39
1
Easy way to remove memory leak: grepnew
and remove those lines
– Caleth
Mar 22 at 11:44
1
@john if it stores pointers then yes it is.
– Sombrero Chicken
Mar 22 at 11:48
|
show 4 more comments
Is it safe to delete an element in a std::priority_queue
and pop it afterwards or does the pop try to do anything with the reference other than discarding it?
while(!priority_queue.empty())
delete priority_queue.top();
priority_queue.pop();
c++ memory-management
Is it safe to delete an element in a std::priority_queue
and pop it afterwards or does the pop try to do anything with the reference other than discarding it?
while(!priority_queue.empty())
delete priority_queue.top();
priority_queue.pop();
c++ memory-management
c++ memory-management
asked Mar 22 at 11:31
JohnJohn
249213
249213
3
What are you storing in your priority queue?
– Konrad Rudolph
Mar 22 at 11:36
The priority queue contains std::pairs<int, double>. I'm trying to fix a memory leak in somebody's code.
– John
Mar 22 at 11:38
2
How are you even able to calldelete
then if you're not storing pointers?
– Sombrero Chicken
Mar 22 at 11:39
1
Easy way to remove memory leak: grepnew
and remove those lines
– Caleth
Mar 22 at 11:44
1
@john if it stores pointers then yes it is.
– Sombrero Chicken
Mar 22 at 11:48
|
show 4 more comments
3
What are you storing in your priority queue?
– Konrad Rudolph
Mar 22 at 11:36
The priority queue contains std::pairs<int, double>. I'm trying to fix a memory leak in somebody's code.
– John
Mar 22 at 11:38
2
How are you even able to calldelete
then if you're not storing pointers?
– Sombrero Chicken
Mar 22 at 11:39
1
Easy way to remove memory leak: grepnew
and remove those lines
– Caleth
Mar 22 at 11:44
1
@john if it stores pointers then yes it is.
– Sombrero Chicken
Mar 22 at 11:48
3
3
What are you storing in your priority queue?
– Konrad Rudolph
Mar 22 at 11:36
What are you storing in your priority queue?
– Konrad Rudolph
Mar 22 at 11:36
The priority queue contains std::pairs<int, double>. I'm trying to fix a memory leak in somebody's code.
– John
Mar 22 at 11:38
The priority queue contains std::pairs<int, double>. I'm trying to fix a memory leak in somebody's code.
– John
Mar 22 at 11:38
2
2
How are you even able to call
delete
then if you're not storing pointers?– Sombrero Chicken
Mar 22 at 11:39
How are you even able to call
delete
then if you're not storing pointers?– Sombrero Chicken
Mar 22 at 11:39
1
1
Easy way to remove memory leak: grep
new
and remove those lines– Caleth
Mar 22 at 11:44
Easy way to remove memory leak: grep
new
and remove those lines– Caleth
Mar 22 at 11:44
1
1
@john if it stores pointers then yes it is.
– Sombrero Chicken
Mar 22 at 11:48
@john if it stores pointers then yes it is.
– Sombrero Chicken
Mar 22 at 11:48
|
show 4 more comments
2 Answers
2
active
oldest
votes
After clarification from OP saying that the queue doesn't container any pointers, just objects :
You don't have to call delete
at all. Just call pop()
only in your while loop. The container deals with deallocation.
add a comment |
So this page is useful:
http://www.cplusplus.com/reference/queue/priority_queue/pop/
What it says is that the objects in the queue are destructed.
However if the object doesn't actually have a destructor that deletes its content then you have to delete the content yourself. But that is not OO programming, it is just lazy use of a container.
Note that say a pair of objects that both do have proper destructors will call both destructors correctly.
So in general, no, you shouldn't be deleting the objects explicitly, but is you have lazily written a container of raw pointers, or pairs of pointers, then you will have to, because you wont benefit from destructor calling.
No. Ops queue contains pointers, not actual objects.
– Sombrero Chicken
Mar 22 at 11:35
@SombreroChicken Who knows? And even if they are pointers they don’t necessarily need to be deallocated.
– Konrad Rudolph
Mar 22 at 11:35
@KonradRudolph OP callingdelete
on makes it pretty clear.
– Sombrero Chicken
Mar 22 at 11:36
@SombreroChicken Nothing in this question is particularly clear.
– Konrad Rudolph
Mar 22 at 11:36
@KonradRudolph my vision is 20/20
– Sombrero Chicken
Mar 22 at 11:36
|
show 3 more comments
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%2f55298682%2fdeallocating-from-c-priority-queue%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
After clarification from OP saying that the queue doesn't container any pointers, just objects :
You don't have to call delete
at all. Just call pop()
only in your while loop. The container deals with deallocation.
add a comment |
After clarification from OP saying that the queue doesn't container any pointers, just objects :
You don't have to call delete
at all. Just call pop()
only in your while loop. The container deals with deallocation.
add a comment |
After clarification from OP saying that the queue doesn't container any pointers, just objects :
You don't have to call delete
at all. Just call pop()
only in your while loop. The container deals with deallocation.
After clarification from OP saying that the queue doesn't container any pointers, just objects :
You don't have to call delete
at all. Just call pop()
only in your while loop. The container deals with deallocation.
answered Mar 22 at 11:48
Sombrero ChickenSombrero Chicken
24.9k33381
24.9k33381
add a comment |
add a comment |
So this page is useful:
http://www.cplusplus.com/reference/queue/priority_queue/pop/
What it says is that the objects in the queue are destructed.
However if the object doesn't actually have a destructor that deletes its content then you have to delete the content yourself. But that is not OO programming, it is just lazy use of a container.
Note that say a pair of objects that both do have proper destructors will call both destructors correctly.
So in general, no, you shouldn't be deleting the objects explicitly, but is you have lazily written a container of raw pointers, or pairs of pointers, then you will have to, because you wont benefit from destructor calling.
No. Ops queue contains pointers, not actual objects.
– Sombrero Chicken
Mar 22 at 11:35
@SombreroChicken Who knows? And even if they are pointers they don’t necessarily need to be deallocated.
– Konrad Rudolph
Mar 22 at 11:35
@KonradRudolph OP callingdelete
on makes it pretty clear.
– Sombrero Chicken
Mar 22 at 11:36
@SombreroChicken Nothing in this question is particularly clear.
– Konrad Rudolph
Mar 22 at 11:36
@KonradRudolph my vision is 20/20
– Sombrero Chicken
Mar 22 at 11:36
|
show 3 more comments
So this page is useful:
http://www.cplusplus.com/reference/queue/priority_queue/pop/
What it says is that the objects in the queue are destructed.
However if the object doesn't actually have a destructor that deletes its content then you have to delete the content yourself. But that is not OO programming, it is just lazy use of a container.
Note that say a pair of objects that both do have proper destructors will call both destructors correctly.
So in general, no, you shouldn't be deleting the objects explicitly, but is you have lazily written a container of raw pointers, or pairs of pointers, then you will have to, because you wont benefit from destructor calling.
No. Ops queue contains pointers, not actual objects.
– Sombrero Chicken
Mar 22 at 11:35
@SombreroChicken Who knows? And even if they are pointers they don’t necessarily need to be deallocated.
– Konrad Rudolph
Mar 22 at 11:35
@KonradRudolph OP callingdelete
on makes it pretty clear.
– Sombrero Chicken
Mar 22 at 11:36
@SombreroChicken Nothing in this question is particularly clear.
– Konrad Rudolph
Mar 22 at 11:36
@KonradRudolph my vision is 20/20
– Sombrero Chicken
Mar 22 at 11:36
|
show 3 more comments
So this page is useful:
http://www.cplusplus.com/reference/queue/priority_queue/pop/
What it says is that the objects in the queue are destructed.
However if the object doesn't actually have a destructor that deletes its content then you have to delete the content yourself. But that is not OO programming, it is just lazy use of a container.
Note that say a pair of objects that both do have proper destructors will call both destructors correctly.
So in general, no, you shouldn't be deleting the objects explicitly, but is you have lazily written a container of raw pointers, or pairs of pointers, then you will have to, because you wont benefit from destructor calling.
So this page is useful:
http://www.cplusplus.com/reference/queue/priority_queue/pop/
What it says is that the objects in the queue are destructed.
However if the object doesn't actually have a destructor that deletes its content then you have to delete the content yourself. But that is not OO programming, it is just lazy use of a container.
Note that say a pair of objects that both do have proper destructors will call both destructors correctly.
So in general, no, you shouldn't be deleting the objects explicitly, but is you have lazily written a container of raw pointers, or pairs of pointers, then you will have to, because you wont benefit from destructor calling.
edited Mar 22 at 11:50
answered Mar 22 at 11:33
Gem TaylorGem Taylor
2,279220
2,279220
No. Ops queue contains pointers, not actual objects.
– Sombrero Chicken
Mar 22 at 11:35
@SombreroChicken Who knows? And even if they are pointers they don’t necessarily need to be deallocated.
– Konrad Rudolph
Mar 22 at 11:35
@KonradRudolph OP callingdelete
on makes it pretty clear.
– Sombrero Chicken
Mar 22 at 11:36
@SombreroChicken Nothing in this question is particularly clear.
– Konrad Rudolph
Mar 22 at 11:36
@KonradRudolph my vision is 20/20
– Sombrero Chicken
Mar 22 at 11:36
|
show 3 more comments
No. Ops queue contains pointers, not actual objects.
– Sombrero Chicken
Mar 22 at 11:35
@SombreroChicken Who knows? And even if they are pointers they don’t necessarily need to be deallocated.
– Konrad Rudolph
Mar 22 at 11:35
@KonradRudolph OP callingdelete
on makes it pretty clear.
– Sombrero Chicken
Mar 22 at 11:36
@SombreroChicken Nothing in this question is particularly clear.
– Konrad Rudolph
Mar 22 at 11:36
@KonradRudolph my vision is 20/20
– Sombrero Chicken
Mar 22 at 11:36
No. Ops queue contains pointers, not actual objects.
– Sombrero Chicken
Mar 22 at 11:35
No. Ops queue contains pointers, not actual objects.
– Sombrero Chicken
Mar 22 at 11:35
@SombreroChicken Who knows? And even if they are pointers they don’t necessarily need to be deallocated.
– Konrad Rudolph
Mar 22 at 11:35
@SombreroChicken Who knows? And even if they are pointers they don’t necessarily need to be deallocated.
– Konrad Rudolph
Mar 22 at 11:35
@KonradRudolph OP calling
delete
on makes it pretty clear.– Sombrero Chicken
Mar 22 at 11:36
@KonradRudolph OP calling
delete
on makes it pretty clear.– Sombrero Chicken
Mar 22 at 11:36
@SombreroChicken Nothing in this question is particularly clear.
– Konrad Rudolph
Mar 22 at 11:36
@SombreroChicken Nothing in this question is particularly clear.
– Konrad Rudolph
Mar 22 at 11:36
@KonradRudolph my vision is 20/20
– Sombrero Chicken
Mar 22 at 11:36
@KonradRudolph my vision is 20/20
– Sombrero Chicken
Mar 22 at 11:36
|
show 3 more comments
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%2f55298682%2fdeallocating-from-c-priority-queue%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
3
What are you storing in your priority queue?
– Konrad Rudolph
Mar 22 at 11:36
The priority queue contains std::pairs<int, double>. I'm trying to fix a memory leak in somebody's code.
– John
Mar 22 at 11:38
2
How are you even able to call
delete
then if you're not storing pointers?– Sombrero Chicken
Mar 22 at 11:39
1
Easy way to remove memory leak: grep
new
and remove those lines– Caleth
Mar 22 at 11:44
1
@john if it stores pointers then yes it is.
– Sombrero Chicken
Mar 22 at 11:48