Tim S
Tim S

Reputation: 23

advice on memory management when using STL priority_queue

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? */
  }
}

Upvotes: 2

Views: 2560

Answers (1)

juanchopanza
juanchopanza

Reputation: 227618

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

Upvotes: 2

Related Questions