Mathijs
Mathijs

Reputation: 357

Does STL's priority_queue re-allocate if the container does not?

I'm a little confused about STL's priority_queue and how it allocates memory. Am I correct in assuming that it does not need to dynamically allocate memory itself, but only the underlying container may?

This would mean using something like Boost's static_vector as container would result in a priority_queue that never allocates once it's been set up. I need something like a priority_queue in a real-time application where I don't want to allocate memory run-time, hence the question.

Upvotes: 2

Views: 60

Answers (1)

Nicol Bolas
Nicol Bolas

Reputation: 473537

priority_queue is a container adapter; it's just an interface around the container itself. Therefore, its behavior is only that of the container it is given. Specifically, it requires that the container is a random-access container (and therefore supports the usual container stuff like begin/end/insert/etc).

As such, allocation behavior is based on the behavior of the underlying container. If removing an element from the container can deallocate memory, or if inserting after a removal allocates memory, then those operations will deallocate or allocate memory.

Upvotes: 6

Related Questions