473,385 Members | 1,856 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,385 software developers and data experts.

will std::deque not reallocate?

std::deque<intd;
d.push_front(13);
int* p13 = &d.begin();
d.push_front(1);
d.push_back(2);

Can I be safe, that p13 points to 13? I mean, unless I erase the 13,
of course.
--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

Dec 7 '06 #1
8 2943

Gernot Frisch wrote:
std::deque<intd;
d.push_front(13);
int* p13 = &d.begin();
d.push_front(1);
d.push_back(2);

Can I be safe, that p13 points to 13? I mean, unless I erase the 13,
of course.
I don't have the standard in front of me at the moment but I'm pretty
sure that push_front and push_back do not invalidate iterators or
pointers in a deque.

Dec 7 '06 #2
why not try it out on ur own ? :-(

Gernot Frisch wrote:
std::deque<intd;
d.push_front(13);
int* p13 = &d.begin();
d.push_front(1);
d.push_back(2);

Can I be safe, that p13 points to 13? I mean, unless I erase the 13,
of course.
--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}
Dec 7 '06 #3
On Thu, 7 Dec 2006 18:00:33 +0100 in comp.lang.c++, "Gernot Frisch"
<Me@Privacy.netwrote,
>std::deque<intd;
d.push_front(13);
int* p13 = &d.begin();
int* p13 = &*(d.begin());
>d.push_front(1);
d.push_back(2);

Can I be safe, that p13 points to 13? I mean, unless I erase the 13,
of course.
push_front() and push_back() on deque do not invalidate the pointer to
the element. Please be careful, inserting or erasing in the middle (not
at front or back) of the deque will invalidate the pointer. Review
standard section 23.2.1.3 deque modifiers. std::list might be a safer
choice.

Dec 7 '06 #4
On 7 Dec 2006 09:08:22 -0800 in comp.lang.c++, "Katharine Meyers"
<k_****************@yahoo.comwrote,
>why not try it out on ur own ? :-(
Because "try it" almost never answers the question "is it guaranteed to
be safe?".

Dec 7 '06 #5
Gernot Frisch wrote:
std::deque<intd;
d.push_front(13);
int* p13 = &d.begin();
d.push_front(1);
d.push_back(2);

Can I be safe, that p13 points to 13? I mean, unless I erase the 13,
of course.


Cited from the docs of SGI STL:

The semantics of iterator invalidation for deque is as follows. Insert
(including push_front and push_back) invalidates all iterators that
refer to a deque. Erase in the middle of a deque invalidates all
iterators that refer to the deque. Erase at the beginning or end of a
deque (including pop_front and pop_back) invalidates an iterator only
if it points to the erased element.

So there are STL libraries around, where no guarantees are given for
references. In an ideal world an invalidated iterator means invalidated
reference.

Microsoft C++ seems to invalidate iterators, but keep up references to
elements on insert and delete at the beginning, so there you might have
luck with the construction above.

I think you should use list instead of deque to be sure you have
reliable and portable code.

Bernd Strieder

Dec 7 '06 #6
Gernot Frisch wrote:
std::deque<intd;
d.push_front(13);
int* p13 = &d.begin();
You probably meant int* p13 = &*d.begin();
or int* p13 = &d.front();
d.push_front(1);
d.push_back(2);

Can I be safe, that p13 points to 13?
I think it is, since std::deque is not like std::vector.
It really supports constant time insertion at the beginning and the end,
not just amortized constant time insertion at the end.
That's because std::deque organizes its memory in chunks.
Dec 7 '06 #7
So there are STL libraries around, where no guarantees are given for
references.
Thank you for clearing up.
Dec 8 '06 #8
Gernot Frisch wrote:
std::deque<intd;
d.push_front(13);
int* p13 = &d.begin();
d.push_front(1);
d.push_back(2);

Can I be safe, that p13 points to 13? I mean, unless I erase the 13,
of course.
In [23.1.1/Table 68], the standard defines the semantics of push_front() and
push_back() in terms of insert. Whether those operations invalidate
iterators, pointer, or references then depends on what insert() does. Here
is what the standard says about insert() for std::deque [23.2.1.3/1]:

iterator insert(iterator position, const T& x);
void insert(iterator position, size_type n, const T& x);
template <class InputIterator>
void insert(iterator position, InputIterator first, InputIterator last);

Effects: An insert in the middle of the deque invalidates all the
iterators and references to elements of the deque. An insert at either end
of the deque invalidates all the iterators to the deque, but has no effect
on the validity of references to elements of the deque.
Best

Kai-Uwe Bux
Dec 8 '06 #9

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

Similar topics

0
by: Dan Trowbridge | last post by:
He everyone, I just getting started with .NET and I am having a porting problem. I get and error in code that lookes like this (really stripped down but you get the idea)... class dt {...
1
by: bartek | last post by:
Hi, I'm sorry for asking such a simple question. I just would like to know what's the standard-defined behaviour when doing a push_back() on a std::deque. TIA, b
7
by: Dan Trowbridge | last post by:
He everyone, I am just getting started with .NET and I am having a porting problem. I get and error in code that lookssomething like this (really stripped down but you get the idea)... class...
7
by: DevNull | last post by:
Hello everyone, I decided to pick c++ back up after not really having used it much in 10 years. Just as a point of refference, there was no standard C++ last time I used regularly. Anyways...
2
by: bb | last post by:
Hi, Is there any specific reason why std::deque's pop_front & pop_back does not return the removed object? It could return the removed object by value (possibly atomically?) Cheers.
29
by: NvrBst | last post by:
I've read a bit online seeing that two writes are not safe, which I understand, but would 1 thread push()'ing and 1 thread pop()'ing be thread-safe? Basically my situation is the follows: ...
3
by: Peng Yu | last post by:
Hi, I don't understand why rbegin() -rend() gives a negative number. Since rbegin() + 1 gives the one before the last element, I think rbegin() - rend() should give a positive number. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...

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.