Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

What's the point of std::queue::pop() if I can't store the result?

296 views
Skip to first unread message

elmazzun

unread,
Apr 10, 2016, 10:24:10 AM4/10/16
to
Hi everybody, there is something that I don't understand
about popping from std::queue.
I pushed successfully a custom struct of mine in a queue,
like this:

data_packet pkt; // typedef'd struct of mine
q.push(pkt); // push() goes fine

What I don't get is how to store the result of a popped
element from my queue, since it is "void pop()".
I read about queue::front, which returns a reference to
"the oldest element in the queue and the same element that
is popped out from the queue when queue::pop is called."
Well, if front() returns a reference&, I made like this:

data_packet *pkt;
if (!q.empty()) {
pkt = q.front();
q.pop();
}

but the compiler says:
error: cannot convert 'data_pkt' to 'data_packet* {aka data_pkt*}' in assignment.

I think it returns this error because I pushed in my queue
a data_packet struct, not a pointer to it.
But, how can I retrieve a struct from my queue and
store it, not just pop() it and lose it?

Daniel

unread,
Apr 10, 2016, 10:56:04 AM4/10/16
to
On Sunday, April 10, 2016 at 10:24:10 AM UTC-4, elmazzun wrote:
> I pushed successfully a custom struct of mine in a queue,
> like this:
>
> data_packet pkt; // typedef'd struct of mine
> q.push(pkt); // push() goes fine
>
> What I don't get is how to store the result of a popped
> element from my queue, since it is "void pop()".

data_packet first = q.front();
q.pop();

> I read about queue::front, which returns a reference ...
>
> data_packet *pkt;
> if (!q.empty()) {
> pkt = q.front();
> q.pop();
> }
>

If you replaced

data_packet *pkt;

with

data_packet& pkt;

it would compile, but it wouldn't work, as the reference would be
invalided by q.pop();

Daniel

elmazzun

unread,
Apr 10, 2016, 11:13:05 AM4/10/16
to

> data_packet first = q.front();
> q.pop();
>
>
> If you replaced
>
> data_packet *pkt;
>
> with
>
> data_packet& pkt;
>
> it would compile, but it wouldn't work, as the reference would be
> invalided by q.pop();
>

That's what I was doing, indeed it compiled but the fields in my
retrieved struct shows orrible numbers.
Even commenting the pop() instruction did not make it work...

Öö Tiib

unread,
Apr 10, 2016, 11:49:46 AM4/10/16
to
On Sunday, 10 April 2016 18:13:05 UTC+3, elmazzun wrote:
> > data_packet first = q.front();
> > q.pop();
> >
> >
> > If you replaced
> >
> > data_packet *pkt;
> >
> > with
> >
> > data_packet& pkt;
> >
> > it would compile, but it wouldn't work, as the reference would be
> > invalided by q.pop();
> >
>
> That's what I was doing, indeed it compiled but the fields in my
> retrieved struct shows horrible numbers.
> Even commenting the pop() instruction did not make it work...

That sounds like your code has some more issues than that 'pop'.

The logic is that if copy constructor of popped element throws during
'pop' then first the implementation of 'pop' and after that you will
have quite bad situation. So no copies are made during 'pop'. Think
about it.

Therefore you should 'pop()' *after* you have done everything that you
want with the element. Including (if you need to) copying or moving
from it.

After 'pop' the element is gone and destroyed. Debug versions of some implementations also fill the memory where it was with most unlikely
garbage to ensure that it does not work and draws your attention.

elmazzun

unread,
Apr 10, 2016, 12:28:51 PM4/10/16
to
You are right, I had to front() the struct from the queue
and ONLY after I read the values in the retrieved struct
pop() it.
Thanks to both of you, this was so stupid...still learning,
I'm from 2 years of C development and I still have to
master C++.
0 new messages