Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > std::queue and memory

Reply
Thread Tools

std::queue and memory

 
 
Brad
Guest
Posts: n/a
 
      08-09-2010
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.

 
Reply With Quote
 
 
 
 
Alf P. Steinbach /Usenet
Guest
Posts: n/a
 
      08-09-2010
* 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

--
blog at <url: http://alfps.wordpress.com>
 
Reply With Quote
 
 
 
 
Jorgen Grahn
Guest
Posts: n/a
 
      08-10-2010
On Mon, 2010-08-09, Brad wrote:
> 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

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
Brad
Guest
Posts: n/a
 
      08-10-2010
Thanks for the tips Alf and Jorgen. I figured out what I was doing
wrong. I appreciate the information.

Brad

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic memory allocation and memory leak... s.subbarayan C Programming 10 03-22-2005 02:48 PM
Roadstor and Sony Memory Stick Pro Memory card arouth@radiology.umsmed.edu Digital Photography 0 01-13-2005 05:23 AM
Differences between Sony Memory Stick & memory Stick Pro vs Memory Stick Duo? zxcvar Digital Photography 3 11-28-2004 10:48 PM
what is working memory and main memory? Lee Java 8 01-24-2004 10:56 AM
How I free memory and return value from that memory area? sam C++ 2 06-27-2003 01:29 PM



Advertisments