C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Stack/Queue reference being invalidated?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Dhruv Matani
Guest





PostPosted: Wed Apr 21, 2004 9:27 am    Post subject: Stack/Queue reference being invalidated? Reply with quote



Hello,
Consider this piece of code:

stack<int> s;
s.push(1);
int& iref = s.top();

s.push(2);
s.push(55);

Will the reference iref be valid even now?
I guess not because the underlying container's iterators do get
invalidated? Or maybe I am wrong?

--
Regards,
-Dhruv.

Proud to be a Vegetarian.
http://www.vegetarianstarterkit.com/
http://www.vegkids.com/vegkids/index.html


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
llewelly
Guest





PostPosted: Thu Apr 22, 2004 12:20 am    Post subject: Re: Stack/Queue reference being invalidated? Reply with quote



"Dhruv Matani" <dhruvbird (AT) gmx (DOT) net> writes:

Quote:
Hello,
Consider this piece of code:

stack<int> s;
s.push(1);
int& iref = s.top();

s.push(2);
s.push(55);

Will the reference iref be valid even now?
[snip]


Niether deque nor list invalidate references or pointers to elements
on a call to push_back(). (See 23.2.2.3/1 for list. The chapter
and verse for deque I cannot find, but 23.2.1.3/1 should make it
clear. )

vector *does* invalidate references to elements on push_back iff a
re-allocation is required. You can use reserve and capacity to
affect this.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
moswald
Guest





PostPosted: Thu Apr 22, 2004 3:09 pm    Post subject: Re: Stack/Queue reference being invalidated? Reply with quote



According to the VC7 help, deque reallocation occurs when a member function
must insert or erase elements of the sequence: if an element is inserted
at the first position of the deque, then all iterators, but no references,
that designate existing elements become invalid.

Assuming the stack class you're using does indeed use the deque as it's
underlying container object, your push_back will invalidate any iterators
to the position that stores '1', but the reference to '1' is still valid.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Anthony Smith
Guest





PostPosted: Thu Apr 22, 2004 3:16 pm    Post subject: Re: Stack/Queue reference being invalidated? Reply with quote

Quote:
Hello,
Consider this piece of code:

stack<int> s;
s.push(1);
int& iref = s.top();

s.push(2);
s.push(55);

Will the reference iref be valid even now?

This reference could well be invalid. It all depends on the implementation
of stack in the standard library that came with your compiler. The Standard
(23.2.3.3) says that any sequence supporting .back(), push_back() and
pop_back() can be used to implement stack, listing vector, list and deque in
particular. So here's one possible situation that will invalidate your
reference:

The implemetor decided to used a vector to build your stack. Sometime after
you take your reference, the vector runs out of space while you're pushing
and grows, reallocating more memory. This new memory is probably not going
to be in the same place as the old memory so now your reference refers to
memory that is either no-longer allocated or has since been allocated by
something else.

The general guideline I stick to is simply always to assume that I can't
know anything about where or how a container stores my data and that my data
may move around more than I'd care to know about. If you code "as-if" this
were the case (which it isn't quite, but it's not that far off either) then
you should never run into this sort of problem.

Hope this helps

Anthony Smith



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
John Potter
Guest





PostPosted: Fri Apr 23, 2004 12:39 am    Post subject: Re: Stack/Queue reference being invalidated? Reply with quote

On 21 Apr 2004 05:27:30 -0400, "Dhruv Matani" <dhruvbird (AT) gmx (DOT) net> wrote:

Quote:
stack<int> s;

This is stack<int, deque

Quote:
s.push(1);
int& iref = s.top();

s.push(2);
s.push(55);

Will the reference iref be valid even now?

Yes.

Quote:
I guess not because the underlying container's iterators do get
invalidated? Or maybe I am wrong?

The iterators might but not references for push_back on deque.

John

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Carl Barron
Guest





PostPosted: Fri Apr 23, 2004 11:52 pm    Post subject: Re: Stack/Queue reference being invalidated? Reply with quote

In article <nPChc.35548$Y%6.4633242 (AT) wards (DOT) force9.net>, Anthony Smith
<atsmith6 (AT) yahoo (DOT) co.uk> wrote:

Quote:
Hello,
Consider this piece of code:

stack<int> s;
s.push(1);
int& iref = s.top();

s.push(2);
s.push(55);

Will the reference iref be valid even now?


[23.2] states
template <class T,class Container = deqie class stack;

Therefore the underlying container is a deque<int>. The reference is
valid, as written. The implimenter is not premitted to change default
arguments. If the OP wrote stack<int,vector then the reference
is not guaranteed.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.