std::queue and memory

B

Brad

Before getting into the details, I'll start by saying that I've never
had need to use new and delete much before now and all the examples I
read about new/delete use types such as int and char. So if some of my
observations seem silly, that's probably why.

std::queue<std::string>* Q()
{
std::queue<std::string>* q = new std::queue<std::string>;
return q;
}

std::queue<std::string>* q = Q();

When I do q.push("a_string") memory is taken from the heap. q.push()
returns void. How can I tell if it fails due to lack of memory?

Also, it confuses me that while push() consumes memory that pop() does
not release it. The only way to release memory is to delete q. So if
one has more strings than memory that must go into the queue, then one
has to new, push(), pop() and then delete the queue. Repeat those
things over and over until all strings are processed. Is there a
better way?

I envisioned a container that consumed X amount of memory and could
push() and pop() (allocating and deallocating memory on the fly). So
that each time a pop() occurs a new push() could occur and the memory
would remain the same (provided the strings are the same length).

Any information or good reading material on this is much appreciated.
 
A

Alf P. Steinbach /Usenet

* Brad, on 09.08.2010 14:12:
Before getting into the details, I'll start by saying that I've never
had need to use new and delete much before now and all the examples I
read about new/delete use types such as int and char. So if some of my
observations seem silly, that's probably why.

std::queue<std::string>* Q()
{
std::queue<std::string>* q = new std::queue<std::string>;
return q;
}

std::queue<std::string>* q = Q();

This example seems irrelevant to the following questions.

In particular, with q a pointer the period '.' would be invalid.


When I do q.push("a_string") memory is taken from the heap. q.push()
returns void. How can I tell if it fails due to lack of memory?

A std::bad_alloc exception is thrown.

Also, it confuses me that while push() consumes memory that pop() does
not release it.

pop() may or may not release memory allocated internally by the queue.

The only way to release memory is to delete q.

Just let the queue object go out of scope, or swap with an empty one:

typedef std::queue< std::string > StringQueue;

StringQueue q;

// Add strings to q

// Free all memory
StringQueue().swap( q );

// Add more strings

So if
one has more strings than memory that must go into the queue, then one
has to new, push(), pop() and then delete the queue.

If you don't have enough memory then you don't have enough memory.

Repeat those
things over and over until all strings are processed. Is there a
better way?

I envisioned a container that consumed X amount of memory and could
push() and pop() (allocating and deallocating memory on the fly). So
that each time a pop() occurs a new push() could occur and the memory
would remain the same (provided the strings are the same length).

A queue does reuse previously allocated memory.

This memory is overhead.

It does not depend on the lengths of the strings (each std::string allocates its
own memory for its data).

Any information or good reading material on this is much appreciated.

A C++ book?


Cheers & hth.,

- Alf
 
J

Jorgen Grahn

Before getting into the details, I'll start by saying that I've never
had need to use new and delete much before now and all the examples I
read about new/delete use types such as int and char.

As a side note, those are the most useless news and deletes, alongside
new and delete on arrays. I don't use new a lot, but when I do it's
almost always on class types.

Possibly that means you have been reading bad examples (or not enough
of them).
So if some of my
observations seem silly, that's probably why.

Well, as Alf noted elsewhere your question isn't really related to
your usage of new and delete, but to how std::queue handles its own
memory.

/Jorgen
 
B

Brad

Thanks for the tips Alf and Jorgen. I figured out what I was doing
wrong. I appreciate the information.

Brad
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top