Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Valid pointer to map element after erase

Reply
Thread Tools

Valid pointer to map element after erase

 
 
gfiuni2
Guest
Posts: n/a
 
      08-24-2006

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.

 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      08-24-2006
gfiuni2 wrote:

>
> Hi,
>
> Given the code below:
>
> #include <iostream.h>


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

#include <iostream>

> #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
 
Reply With Quote
 
 
 
 
amirkam1@yahoo.com
Guest
Posts: n/a
 
      08-24-2006
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.

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

 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      08-24-2006
wrote:

> 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?


> 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

 
Reply With Quote
 
amirkam1@yahoo.com
Guest
Posts: n/a
 
      08-24-2006

amirk...@yahoo.com wrote:
> 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.
>
> 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



Oops, I missed that the element being deleted and stored in pointer are
different.

 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      08-24-2006
gfiuni2 wrote:
> Hi,
>
> Given the code below:
>
> #include <iostream.h>

non standard header. Use #include <iostream>

> #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;

std::cout << *ptr < std::endl;
> }
>
>
>
> Is ptr a valid pointer after erasing elements from map?
>


Yes. Pointers and iterators into an associative container are valid
after an erase (as long as you're not looking at the erased elements).

Side note about my earlier comments. You obviously are aware of the
Standard headers, as you're using <string> and <map>. Why are you still
using iostream.h?

 
Reply With Quote
 
Jose Luis
Guest
Posts: n/a
 
      08-25-2006

> Side note about my earlier comments. You obviously are aware of the
> Standard headers, as you're using <string> and <map>. Why are you still
> using iostream.h?


I am working on this enviroment:

m3vmsa3.closedeb /tmp > aCC -V
aCC: HP ANSI C++ B3910B A.03.27
m3vmsa3.closedeb /tmp > uname -a
HP-UX m3vmsa3 B.11.00 U 9000/800 178951547 unlimited-user license



If I don't append ".h" to any input/output header the compiler
complains:

m3vmsa3.closedeb /tmp > aCC kk.c
Error 112: "kk.c", line 2 # Include file <iostream> not found.
#include <iostream>

Regards,
Jose Luis

 
Reply With Quote
 
Marcus Kwok
Guest
Posts: n/a
 
      08-25-2006
Jose Luis <> wrote:
>
>> Side note about my earlier comments. You obviously are aware of the
>> Standard headers, as you're using <string> and <map>. Why are you still
>> using iostream.h?

>
> I am working on this enviroment:
>
> m3vmsa3.closedeb /tmp > aCC -V
> aCC: HP ANSI C++ B3910B A.03.27
> m3vmsa3.closedeb /tmp > uname -a
> HP-UX m3vmsa3 B.11.00 U 9000/800 178951547 unlimited-user license
>
>
> If I don't append ".h" to any input/output header the compiler
> complains:
>
> m3vmsa3.closedeb /tmp > aCC kk.c
> Error 112: "kk.c", line 2 # Include file <iostream> not found.
> #include <iostream>


<OT>
Try using the '-AA' parameter for aCC, which turns on
standards-compliant mode.
</OT>

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
STL: valid pointer to element of a list or vector container? Rui Maciel C++ 6 05-11-2010 01:11 AM
Once again pointer to first element vs pointer to array cast topointer to element Szabolcs Borsanyi C Programming 6 05-23-2008 11:06 AM
erase vs. erase al.cpwn@gmail.com C++ 7 03-30-2006 11:45 AM
How do i erase all information in my orkut and erase the link to the orkut account? mountbatten@gmail.com Computer Support 1 10-31-2005 12:03 AM
How do I erase a file that wont let me erase it? Dale Custer Computer Support 4 07-06-2003 09:48 AM



Advertisments