473,804 Members | 3,607 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2617
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
1953
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 example, how many objects can be stored in the vector<object>? 2. I want to store many thousand objects into a container, and use "find", "sort" very often. Which standard container should be used in term of efficiency and size limit?
6
2468
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 (the map constructor calls), saying that I'm trying to take the address of a constructor. Another compiler complains about all four of the last lines, just saying "invalid use of class". What is the correct syntax for calling a container...
2
1620
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 i googled i found out people saying about memory leaking. Is this true ?
7
20015
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 cost. STL vector is suitable for removing form tail? or it is as costly as removing from middle? any other std container to serve this purpose? (note , I dont need linked list implementation of any container, as I want random access)
3
1538
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 given class over the pulled item. That is: class lazy(object): def __getitem__(self,index):
18
2956
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 container would be most suited. Am i correct in my assumptions Problem Reocrds are already stored with a unique identity in a binary format.The structure is roughly ----------------------
1
1373
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
2070
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 I had is what does the OS do when an application requests for memory, for ex from a code using stls? Does the application get what it requests ? and where does the memory provided reside, will it be in the memory allocated to the application? or...
18
1825
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
2043
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 there any other no-overhead ways that a contained class can access its container? The obvious choice of passing (a pointer or a reference to the container) to the contained class is not a no-overhead solution, it requires both memory and time. I am...
0
9706
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10571
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10075
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9143
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7615
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6851
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5520
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3815
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.