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;








0















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();










share|improve this question

















  • 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

















0















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();










share|improve this question

















  • 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













0












0








0








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();










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 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












  • 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







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












2 Answers
2






active

oldest

votes


















4














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.






share|improve this answer






























    1














    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.






    share|improve this answer

























    • 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 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











    • @KonradRudolph my vision is 20/20

      – Sombrero Chicken
      Mar 22 at 11:36












    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%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









    4














    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.






    share|improve this answer



























      4














      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.






      share|improve this answer

























        4












        4








        4







        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.






        share|improve this answer













        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 22 at 11:48









        Sombrero ChickenSombrero Chicken

        24.9k33381




        24.9k33381























            1














            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.






            share|improve this answer

























            • 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 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











            • @KonradRudolph my vision is 20/20

              – Sombrero Chicken
              Mar 22 at 11:36
















            1














            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.






            share|improve this answer

























            • 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 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











            • @KonradRudolph my vision is 20/20

              – Sombrero Chicken
              Mar 22 at 11:36














            1












            1








            1







            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.






            share|improve this answer















            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.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            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 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











            • @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











            • @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











            • @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


















            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%2f55298682%2fdeallocating-from-c-priority-queue%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

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현