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 

Valid pointer to map element after erase

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
gfiuni2
Guest





PostPosted: Thu Aug 24, 2006 9:10 am    Post subject: Valid pointer to map element after erase Reply with quote



Hi,

Given the code below:

#include <iostream.h>
#include <string>
#include <map>

int main()
{
map<int, string > m;
string *ptr;

m[1]="one";
m[2]="two";

map<int, string >::iterator it=m.find(1);
ptr=&(it->second);

m.erase(m.find(2));

cout << *ptr << endl;
}



Is ptr a valid pointer after erasing elements from map?


Thanks in advance,
Jose Luis.
Back to top
Kai-Uwe Bux
Guest





PostPosted: Thu Aug 24, 2006 9:10 am    Post subject: Re: Valid pointer to map element after erase Reply with quote



gfiuni2 wrote:

Quote:

Hi,

Given the code below:

#include <iostream.h

This header never was part of the standard. Upgrade to:

#include <iostream>

Quote:
#include <string
#include <map

int main()
{
map<int, string > m;
string *ptr;

m[1]="one";
m[2]="two";

map<int, string >::iterator it=m.find(1);
ptr=&(it->second);

m.erase(m.find(2));

cout << *ptr << endl;
}



Is ptr a valid pointer after erasing elements from map?

According to [23.1.2/8] erase (for associative containers) does not
invalidate references and iterators to elements other than the erased one.
It does not mention pointers, which appears to be an oversight. Pointers
are mentioned along with references and iterators where invalidation is
mentioned elsewhere (e.g., the standard says that swap for containers does
not invalidate pointers, reference, and iterators into a container). So
formally, your program may be undefined; but that appears to be a defect in
the standard and no implementation would actually invalidate the pointer
ptr in your code.


Best

Kai-Uwe Bux
Back to top
Guest






PostPosted: Thu Aug 24, 2006 9:10 am    Post subject: Re: Valid pointer to map element after erase Reply with quote



gfiuni2 wrote:
Quote:
Hi,

Given the code below:

#include <iostream.h
#include <string
#include <map

int main()
{
map<int, string > m;
string *ptr;

m[1]="one";
m[2]="two";

map<int, string >::iterator it=m.find(1);
ptr=&(it->second);

m.erase(m.find(2));

cout << *ptr << endl;
}



Is ptr a valid pointer after erasing elements from map?


Thanks in advance,
Jose Luis.

No, it will be invalid.
The erase is going to destroy the string object stored in the map.

You can try this by adding a user defined object to the map and then
calling erase. The destructor will get called when erase is called.


amir kamerkar
Back to top
Kai-Uwe Bux
Guest





PostPosted: Thu Aug 24, 2006 9:10 am    Post subject: Re: Valid pointer to map element after erase Reply with quote

amirkam1 (AT) yahoo (DOT) com wrote:

Quote:
gfiuni2 wrote:
Hi,

Given the code below:

#include <iostream.h
#include <string
#include <map

int main()
{
map<int, string > m;
string *ptr;

m[1]="one";
m[2]="two";

map<int, string >::iterator it=m.find(1);
ptr=&(it->second);

m.erase(m.find(2));

cout << *ptr << endl;
}



Is ptr a valid pointer after erasing elements from map?


Thanks in advance,
Jose Luis.

No, it will be invalid.
The erase is going to destroy the string object stored in the map.

Did you notice that ptr points to the string whose key is 1 whereas the item
erased is the one whose key is 2?


Quote:
You can try this by adding a user defined object to the map and then
calling erase. The destructor will get called when erase is called.


Best

Kai-Uwe Bux
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) 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.