Valid pointer to map element after erase

Discussion in 'C++' started by gfiuni2, Aug 24, 2006.

  1. gfiuni2

    gfiuni2 Guest

    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.
    gfiuni2, Aug 24, 2006
    #1
    1. Advertising

  2. gfiuni2

    Kai-Uwe Bux Guest

    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
    Kai-Uwe Bux, Aug 24, 2006
    #2
    1. Advertising

  3. gfiuni2

    Guest

    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
    , Aug 24, 2006
    #3
  4. gfiuni2

    Kai-Uwe Bux Guest

    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
    Kai-Uwe Bux, Aug 24, 2006
    #4
  5. gfiuni2

    Guest

    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.
    , Aug 24, 2006
    #5
  6. gfiuni2

    red floyd Guest

    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?
    red floyd, Aug 24, 2006
    #6
  7. gfiuni2

    Jose Luis Guest


    > 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
    Jose Luis, Aug 25, 2006
    #7
  8. gfiuni2

    Marcus Kwok Guest

    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
    Marcus Kwok, Aug 25, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kaede
    Replies:
    1
    Views:
    407
    Thore Karlsen
    Nov 23, 2003
  2. Angus Leeming
    Replies:
    5
    Views:
    2,903
    Howard Hinnant
    Feb 4, 2004
  3. Pieter Thysebaert

    STL std::map erase

    Pieter Thysebaert, May 13, 2004, in forum: C++
    Replies:
    26
    Views:
    14,215
    Robbie Hatley
    May 14, 2004
  4. erase vs. erase

    , Mar 25, 2006, in forum: C++
    Replies:
    7
    Views:
    346
    Pete Becker
    Mar 30, 2006
  5. Szabolcs Borsanyi
    Replies:
    6
    Views:
    647
    Ben Bacarisse
    May 23, 2008
Loading...

Share This Page