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

STL container relocated in memory!? (shifting sands)

Hello -

To begin with, I'm not the most experienced STL use out there but I'm
slowly getting there. One bug(?) that's plagued me of recent is the
way the OS (or is it STL?) appears to relocate a container in memory
if for example the container needs to grow, with the result that any
references (iterators included) to items in such a container cease to
be valid and may return the address 0xCDCDCD or 0xDDDDDD in the
windows environment (I now know what these pointers mean). I haven't
read the standard but I'm guessing it there's something about elements
of some(?) containers needing to be stored contiguously in memory, and
as such the runtime(?) may relocate a container to guarantee this. Is
this so?

Also, lately, its happened that while in the middle of a series of
function calls that do nothing to alter (i.e. grow) the container, an
existing iterator to the container ceases to be valid, apparently the
container has been moved, or so it seems.

Here's a small example I just cooked up:

#include <iostream>
#include <vector>

using namespace std;

int main(int argc, char* argv[])
{
vector<int test;

for( unsigned i = 1; i < 10; i++ )
{
test.push_back(i); cout << "pointer: " << &test.front() << endl;
}
return 0;
}

Output:
pointer: 00335448
pointer: 00335570 // changed
pointer: 00335670
pointer: 00335570
pointer: 00335670
pointer: 00335670
pointer: 003356B8 // changed
pointer: 003356B8
pointer: 003356B8
Oddly, lists are not so affected i.e. when I change the type of test
to list<int , the problem no longer happens:

pointer: 00335488
pointer: 00335488
pointer: 00335488
pointer: 00335488
pointer: 00335488
pointer: 00335488
pointer: 00335488
pointer: 00335488
pointer: 00335488

I guess I've partly figured out the problem, but I'd appreciate more
insight on this problem/feature.

Thanks,

- Olumide
Nov 16 '08 #1
5 2601
I guess I've partly figured out the problem, but I'd appreciate more
insight on this problem/feature.
If I may clarify, although it seems this problem does not affect
lists, I would like to avoid one extreme of not using vectors etc.
Nov 16 '08 #2
Olumide wrote:
Hello -

To begin with, I'm not the most experienced STL use out there but I'm
slowly getting there. One bug(?) that's plagued me of recent is the
way the OS (or is it STL?) appears to relocate a container in memory
if for example the container needs to grow, with the result that any
references (iterators included) to items in such a container cease to
be valid and may return the address 0xCDCDCD or 0xDDDDDD in the
windows environment (I now know what these pointers mean). I haven't
read the standard but I'm guessing it there's something about elements
of some(?) containers needing to be stored contiguously in memory, and
as such the runtime(?) may relocate a container to guarantee this. Is
this so?
The standard says that iterators (and pointers or references to content)
into certain containers (*cough*vector*cough*) are invalidated when the
container grows.
Nov 16 '08 #3
On 2008-11-16 17:52, Olumide wrote:
Hello -

To begin with, I'm not the most experienced STL use out there but I'm
slowly getting there. One bug(?) that's plagued me of recent is the
way the OS (or is it STL?) appears to relocate a container in memory
if for example the container needs to grow, with the result that any
references (iterators included) to items in such a container cease to
be valid and may return the address 0xCDCDCD or 0xDDDDDD in the
windows environment (I now know what these pointers mean). I haven't
read the standard but I'm guessing it there's something about elements
of some(?) containers needing to be stored contiguously in memory, and
as such the runtime(?) may relocate a container to guarantee this. Is
this so?
Yes, some of the standard containers may re-locate the elements which
causes invalidation of any iterators and/or pointers to the elements.
Also, lately, its happened that while in the middle of a series of
function calls that do nothing to alter (i.e. grow) the container, an
existing iterator to the container ceases to be valid, apparently the
container has been moved, or so it seems.
That should not happen, if you do not add or remove elements in the
container the elements should stay in place.
Oddly, lists are not so affected i.e. when I change the type of test
to list<int , the problem no longer happens:
Yes, node-based containers (such as list, set, and map) does not
relocate elements, this is because each element is a node with pointers
to other elements. When you insert/remove elements you only have to
update the pointers and not move the nodes.
I guess I've partly figured out the problem, but I'd appreciate more
insight on this problem/feature.
Any good reference to the standard library should specify if an
operation can invalidate iterators.

--
Erik Wikström
Nov 16 '08 #4
Olumide wrote:
To begin with, I'm not the most experienced STL use out there but I'm
slowly getting there. One bug(?) that's plagued me of recent is the
way the OS (or is it STL?) appears to relocate a container in memory
if for example the container needs to grow
What you need is a good reference manual on the STL containers.

Some containers, as per the C++ standard, guarantee that
pointers/references and/or iterators to existing elements remain valid
even if new elements are added to the container, while other containers
do not make such guarantee. These are completely unambiguously and
precisely documented, and it's just a question of checking for each
container what it does and doesn't guarantee.

Knowing how the containers work internally will make it easier to
remember which guarantees what. (Also knowing their internal workings is
good when you want to choose the most efficient container for a given task.)
Nov 16 '08 #5
On 16/11/08 17:15, red floyd wrote:
Olumide wrote:
>Hello -

To begin with, I'm not the most experienced STL use out there but I'm
slowly getting there. One bug(?) that's plagued me of recent is the
way the OS (or is it STL?) appears to relocate a container in memory
if for example the container needs to grow, with the result that any
references (iterators included) to items in such a container cease to
be valid and may return the address 0xCDCDCD or 0xDDDDDD in the
windows environment (I now know what these pointers mean). I haven't
read the standard but I'm guessing it there's something about elements
of some(?) containers needing to be stored contiguously in memory, and
as such the runtime(?) may relocate a container to guarantee this. Is
this so?

The standard says that iterators (and pointers or references to content)
into certain containers (*cough*vector*cough*) are invalidated when the
container grows.
Vectors invalidate all iterators, pointers and references whenever
reallocation takes place.

Deques invalidate iterators whenever *any* insertion takes place.
Pointers and references to elements remain valid only if inserting to
the front or back.

Definitely worth learning how the STL containers actually work. Then you
can decide which container is most appropriate for what you need to do
with it (sounds like list is the most appropriate for you). You should
get a book like The C++ Standard Library by Josuttis.

--
George Kettleborough
Nov 16 '08 #6

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

Similar topics

7
by: Pat | last post by:
I want to store objects into a C++ container (e.g. vector<object>, deque<object>). Could someone help me the following questions. 1. Does there exist any size limit in the C++ container. For...
6
by: daveb | last post by:
I'm trying to write some code that calls the constructors of STL containers explicitly, and I can't get it to compile. A sample program is below. One compiler complains about the last two lines...
2
by: wolverine | last post by:
Hi Me being a beginner in c++ , i just want to know is their anything special i have to take care when using container inside a container. eg: vector inside a map which is inside a map When...
7
by: toton | last post by:
Hi, I want a circular buffer or queue like container (queue with array implementation). Moreover I want random access over the elements. And addition at tail and remove from head need to be low...
3
by: Cristiano Paris | last post by:
Hi everyone, I'm trying to write a Container which should mimic a list. Basically, the container pulls items on the fly from an unspecified source through a function and returns an instance of a...
18
by: Hunk | last post by:
Would like some advice on the fillowing I have a sorted list of items on which i require to search and retrieve the said item and also modify an item based on its identity. I think an Map stl...
1
by: =?Utf-8?B?dGFzaA==?= | last post by:
when turning on computer comes updll user 32.dll was relocated in memory because dllc:\windows\systems32\hhctrl.ock has occupied an address range reserved for windows systems dlls -- thankyou
2
by: Neo | last post by:
Hello, I have a question on memory allocation for containers. I know what happens when I ask for a container of a certain size or when the container grows to accommodate new elements. The question...
18
by: Goran | last post by:
Hi @ all! Again one small question due to my shakiness of what to use... What is better / smarter? private: vector<MyClass_t* itsVector; OR...
36
by: Peter Olcott | last post by:
So far the only way that I found to do this was by making a single global instance of the container class and providing access to the contained class, through this single global instance. Are...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: 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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.