Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8686383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:50:03+00:00 2026-06-12T22:50:03+00:00

I’m trying to adapt from Java to C++ and am unsure about the correct

  • 0

I’m trying to adapt from Java to C++ and am unsure about the correct way to manage memory when I pop() an item from an STL priority_queue.

Am I supposed to use delete to clean up items removed from the queue that I no longer need? If so, how? If not, why not?

I have written myself a small program to learn how to use a priority_queue (code below). In this program it’s no big deal if there is a memory leak, because it’s so small-scale and ends so quickly. But I want to learn the correct way of doing things so I can write a program that correctly handles a much larger queue without memory leaks.

The thing I do not understand is this: top() returns a reference rather than a pointer. But I can’t use delete on a reference, can I?

Can someone point me in the right direction here?

——————–

struct PathCost{
    int dest;
    int cost;
    PathCost(int _dest, int _cost){
      dest = _dest;
      cost = _cost;
    }
    bool operator<(PathCost other) const;
    bool operator>(PathCost other) const;
};

bool PathCost::operator<(PathCost other) const{
  return cost < other.cost;
}
bool PathCost::operator>(PathCost other) const{
      return cost > other.cost;
}


int main(){

  PathCost pc = PathCost(1, 2);
  pc = PathCost(3, 4);
  PathCost* pcp = new PathCost(5, 6);
  delete pcp;

  priority_queue<PathCost,
                 vector<PathCost>,
                 greater<vector<PathCost>::value_type> > tentativeQ;

  cout << "loading priority queue ...\n";
  tentativeQ.push(PathCost(8, 88));
  tentativeQ.push(PathCost(5, 55));
  tentativeQ.push(PathCost(7, 77));
  tentativeQ.push(PathCost(4, 44));

  cout << "\nlist items on queue in priority order ...\n";
  while (!tentativeQ.empty()){
    pc = tentativeQ.top();
    cout << "dest:" << pc.dest << " cost:" << pc.cost << endl;
    tentativeQ.pop();
    /* DO I NEED TO DO MEMORY CLEANUP AT THIS POINT? */
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T22:50:05+00:00Added an answer on June 12, 2026 at 10:50 pm

    Am I supposed to use delete to clean up items removed from the queue that I no longer need? If so, how? If not, why not?

    You do not need to perform any cleanup, because the priority_queue is holding PathCost objects. When they get removed from the queue, their destructor is called automatically, as per the rules of the language.

    Behind the scenes, the story can be somewhat more complicated. The inserting an item into the priority_queue data structure will, in general, result in the dynamic allocation of a copy of that object. But resource allocation and de-allocation is taken care of by the underlying data structure (by default std::vector) so you do not have to worry about memory management. Standard library containers and container adapters are said to have value semantics.

    The thing I do not understand is this: top() returns a reference rather than a pointer. But I can’t use delete on a reference, can I?

    The priority_queue is said to own the elements it holds, so if you take a reference to one of its elements, you cannot delete it. In fact, you cannot know if it needs to be deleted at all or not. Furthermore, although you have access to references of elements in the queue, you are not forced to keep references to these elements. You can also make your own copy:

    const PathCost& pRef = tentativeQ.top(); // take constant reference to top element
    PathCost p = tentativeQ.top(); // make copy of last element
    

    In the first case, you have to be careful that you do not use that reference after the top element has been removed via a call to pop().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.