Do empty queues occupy memory?

Hi,

I am working on a checker that uses a lot of “int”, “longint” and “real” data type variables. I thought of using empty queues for some variables which I know shall be used seldom. I feel empty queues shall not occupy memory unless a memory is allocated using new() or a data is pushed into them. Is my thinking correct?

Regards,
Vishal

In reply to Vishal D:

Yes they do, but your thinking really depends on your definition of “a lot”.
Although implementation details are not part of the LRM, most queues are implemented with two pointers to the head and tail, which would be initially null. Plus simulators may also implement convenience variables, like the current size. Then there is overhead when allocating each element to the queue.

A dynamic array has less memory overhead, but it’s not zero. And for both queues and dynamic arrays, you there is code and performance overhead when accessing each element.

My advice would be to declare your variables local to the scope where they are used, and make sure they have automatic lifetimes (i.e. inside class methods).

In reply to dave_59:

Thank you Dave for the answer. I’ll follow your advice and attempt to make the seldom used variables local.

Regards,
Vishal