std::queue and memory

Discussion in 'C++' started by Brad, Aug 9, 2010.

  1. Brad

    Brad Guest

    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.
     
    Brad, Aug 9, 2010
    #1
    1. Advertising

  2. * 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>
     
    Alf P. Steinbach /Usenet, Aug 9, 2010
    #2
    1. Advertising

  3. Brad

    Jorgen Grahn Guest

    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 .
     
    Jorgen Grahn, Aug 10, 2010
    #3
  4. Brad

    Brad Guest

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

    Brad
     
    Brad, Aug 10, 2010
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Russell Warren

    Is Queue.Queue.queue.clear() thread-safe?

    Russell Warren, Jun 22, 2006, in forum: Python
    Replies:
    4
    Views:
    704
    Russell Warren
    Jun 27, 2006
  2. Kceiw
    Replies:
    3
    Views:
    1,021
    Jim Langston
    Mar 14, 2006
  3. Frank Millman
    Replies:
    3
    Views:
    357
    Frank Millman
    Feb 9, 2010
  4. Kris
    Replies:
    0
    Views:
    507
  5. bintom
    Replies:
    6
    Views:
    741
    Öö Tiib
    Nov 3, 2012
Loading...

Share This Page