Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Valid pointer to map element after erase (http://www.velocityreviews.com/forums/t456417-valid-pointer-to-map-element-after-erase.html)

gfiuni2 08-24-2006 07:41 AM

Valid pointer to map element after erase
 

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.


Kai-Uwe Bux 08-24-2006 08:38 AM

Re: Valid pointer to map element after erase
 
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

amirkam1@yahoo.com 08-24-2006 08:43 AM

Re: Valid pointer to map element after erase
 
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


Kai-Uwe Bux 08-24-2006 08:53 AM

Re: Valid pointer to map element after erase
 
amirkam1@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.


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


amirkam1@yahoo.com 08-24-2006 09:14 AM

Re: Valid pointer to map element after erase
 

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.


red floyd 08-24-2006 03:36 PM

Re: Valid pointer to map element after erase
 
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?


Jose Luis 08-25-2006 10:28 AM

Re: Valid pointer to map element after erase
 

> 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


Marcus Kwok 08-25-2006 12:56 PM

Re: Valid pointer to map element after erase
 
Jose Luis <jose.luis.fdez.diaz@gmail.com> 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


All times are GMT. The time now is 07:38 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.