473,396 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

STL Queue Data Lifetime

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

Mar 31 '06 #1
2 1309
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();
}
Mar 31 '06 #2
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

Mar 31 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Markus von Ehr | last post by:
Hi, in a data acquisition program I have several threads, one thread grabbing images and another doing some processing. I'd like to make my own image class containing the image and additional...
17
by: Bart Nessux | last post by:
How can one view the contents of a queue? I'd like to verify that the queue has the same number of objects as the list that has been put into it. Also, should one think of a queue as static or...
38
by: Aaron W. LaFramboise | last post by:
Hello, I understand that an easy way to make the standard std::priority_queue stable is by including an integer stamp with each node that is incremented each time a new node is pushed into the...
2
by: Hollywood | last post by:
I have a system in which I have a single thread that places data on a Queue. Then I have one worker thread that waits until data is put on the thread and dequeues the Queue and processes that...
6
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing...
7
by: Michael D. Ober | last post by:
When calling Enqueue, the internal array may need to be reallocated. My question is by how much? In the old MFC array classes, you could tell MFC how many additional elements to add to the array...
3
by: jrpfinch | last post by:
I have a script which is based on the following code. Unfortunately, it only works on Python 2.3 and not 2.5 because there is no esema or fsema attribute in the 2.5 Queue. I am hunting through...
0
by: ecestd | last post by:
I did implement the copy constructor but still have a problem with it. It is not working. What could be wrong? #include "QueueP.h" #include <cassert // for assert #include <new // for...
14
by: AlarV | last post by:
Hello everyone, here is my problem. I have to make a dynamic priority queue,which is a heap, and my book isn't helpful at all, so I have no clues on how to create it.. I read something like a static...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.