STL Queue Data Lifetime

Discussion in 'C++' started by Scott Kilpatrick, Mar 31, 2006.

  1. Maybe I'm going crazy here. I thought that a queue deletes its copied
    elements when the queue is destructed. Can someone explain why the
    following code works?
    --------------------------
    string *p;
    {

    queue<string> q;
    string x = "hello";
    q.push(x);
    p = &q.front(); // p points to "hello" stored in the queue
    cout << "p, *p: " << p << ", " << *p << endl;
    q.pop(); // shouldn't this invalidate p?
    cout << "p, *p: " << p << ", " << *p << endl;

    }
    // the queue is dead, why does this work?
    cout << "p, *p: " << p << ", " << *p << endl;
    -------------------------
    Prints out hello everytime!

    Scott
     
    Scott Kilpatrick, Mar 31, 2006
    #1
    1. Advertising

  2. Scott Kilpatrick wrote:
    > Maybe I'm going crazy here. I thought that a queue deletes its copied
    > elements when the queue is destructed. Can someone explain why the
    > following code works?


    It may "work" but it is undefined. Consider yourself "lucky".

    Here is another example ...

    #include <queue>
    #include <string>
    #include <iostream>

    using namespace std;

    struct Stuff
    {
    Stuff( const string & ix )
    : mx( ix )
    {
    }

    ~Stuff()
    {
    mx = mx + " : dead dead dead - you're just lucky if you see this";
    }

    string mx;
    };

    void f()
    {
    Stuff *p;
    {

    queue<Stuff> q;
    string x = "hello";
    q.push(x);
    p = &q.front(); // p points to "hello" stored in the queue
    cout << "p, *p: " << p << ", " << p->mx << endl;
    q.pop(); // shouldn't this invalidate p?
    cout << "p, *p: " << p << ", " << p->mx << endl;

    }
    // the queue is dead, why does this work?
    cout << "p, *p: " << p << ", " << p->mx << endl;

    }

    int main()
    {
    f();
    }
     
    Gianni Mariani, Mar 31, 2006
    #2
    1. Advertising

  3. Gianni Mariani wrote:
    > It may "work" but it is undefined. Consider yourself "lucky".


    Ah, ok. I knew something was screwy there. I spent some time writing a
    reference counting smart pointer and noticed this during testing. For a
    while I thought I wasted my time!

    Thanks,
    Scott
     
    Scott Kilpatrick, Mar 31, 2006
    #3
    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. Paul L. Du Bois

    Queue.Queue-like class without the busy-wait

    Paul L. Du Bois, Mar 24, 2005, in forum: Python
    Replies:
    29
    Views:
    1,110
    Antoon Pardon
    Apr 4, 2005
  2. Russell Warren

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

    Russell Warren, Jun 22, 2006, in forum: Python
    Replies:
    4
    Views:
    724
    Russell Warren
    Jun 27, 2006
  3. Kceiw
    Replies:
    3
    Views:
    1,047
    Jim Langston
    Mar 14, 2006
  4. Gabriel Rossetti
    Replies:
    3
    Views:
    597
    Jerry Hill
    Apr 25, 2008
  5. Kris
    Replies:
    0
    Views:
    535
Loading...

Share This Page