Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > STL Queue Data Lifetime

Reply
Thread Tools

STL Queue Data Lifetime

 
 
Scott Kilpatrick
Guest
Posts: n/a
 
      03-31-2006
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

 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      03-31-2006
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();
}
 
Reply With Quote
 
 
 
 
Scott Kilpatrick
Guest
Posts: n/a
 
      03-31-2006
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

 
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
Program blocked in Queue.Queue.get and Queue.Queue.put Kris Python 0 01-04-2012 03:46 PM
why does the following with Queue, q.put('\x02', True) not put itin the queue? Gabriel Rossetti Python 3 04-25-2008 03:41 PM
Is Queue.Queue.queue.clear() thread-safe? Russell Warren Python 4 06-27-2006 03:03 PM
what's the difference between #include "queue.h" and #include "queue.cpp" Kceiw C++ 3 03-14-2006 03:01 AM
Queue.Queue-like class without the busy-wait Paul L. Du Bois Python 29 04-04-2005 01:28 PM



Advertisments