std::map::find() throws exception when map is empty?

Discussion in 'C++' started by Matthias Hildebrand, Oct 10, 2003.

  1. Hello,

    std::map< int, MyClass*> mymap;

    it = mymap.find( somekey ) // here happen bad things
    if( it != mymap.end() )
    {
    // do something useful with it->second
    }
    else
    {
    // do nothing
    }

    I traced a strange program crash (unhandled exception on windows,
    something similar on Solaris) down to happen at the second line of
    the code given above which contains the find(). After some
    investigation I found out that it happens when the map is empty.

    This behaviour could be reproduced on Windows 2k and XP using MSVC 6.0
    and 7.0 and finally also on Solaris unsing the Sun CC 5.4.

    I read the documentation as well as books and internet resources but I
    could not find a single statement that on an empty map a find() must
    not be performed.

    Could anybody please give me some information whether this is correct
    or at least well known behaviour? Did I miss something? I do not like
    the idea of preceeding all "critical" find() with some statement like
    if( ! mymap.empty() )...

    Any comments would be appreciated! Thanks!

    Matthias
    Matthias Hildebrand, Oct 10, 2003
    #1
    1. Advertising

  2. "Matthias Hildebrand" <> wrote...
    > std::map< int, MyClass*> mymap;
    >
    > it = mymap.find( somekey ) // here happen bad things
    > if( it != mymap.end() )
    > {
    > // do something useful with it->second
    > }
    > else
    > {
    > // do nothing
    > }
    >
    > I traced a strange program crash (unhandled exception on windows,
    > something similar on Solaris) down to happen at the second line of
    > the code given above which contains the find(). After some
    > investigation I found out that it happens when the map is empty.
    >
    > This behaviour could be reproduced on Windows 2k and XP using MSVC 6.0
    > and 7.0 and finally also on Solaris unsing the Sun CC 5.4.
    >
    > I read the documentation as well as books and internet resources but I
    > could not find a single statement that on an empty map a find() must
    > not be performed.
    >
    > Could anybody please give me some information whether this is correct
    > or at least well known behaviour? Did I miss something? I do not like
    > the idea of preceeding all "critical" find() with some statement like
    > if( ! mymap.empty() )...


    No, this is not correct. As to whether it's a known behaviour, you
    have to check with those who make the library for your compiler.

    Although it has no exception specification, 'find' usually does not
    throw by itself. The reason there is no exception specification is
    that 'find' uses the comparison function for keys, that may throw.

    I understand your frustration about having to use '.empty()' before
    'find', but it may be the only work-around for your particular version
    of the library. Contact the library vendor and let them know about
    your trouble. They should fix it.

    Victor
    Victor Bazarov, Oct 10, 2003
    #2
    1. Advertising

  3. Matthias Hildebrand

    lilburne Guest

    Matthias Hildebrand wrote:

    >>

    > Could anybody please give me some information whether this is correct
    > or at least well known behaviour? Did I miss something? I do not like
    > the idea of preceeding all "critical" find() with some statement like
    > if( ! mymap.empty() )...
    >
    > Any comments would be appreciated! Thanks!
    >


    What was the exception, who threw it, and why?
    lilburne, Oct 10, 2003
    #3
  4. Matthias Hildebrand

    Bob Bell Guest

    (Matthias Hildebrand) wrote in message news:<>...
    > Hello,
    >
    > std::map< int, MyClass*> mymap;
    >
    > it = mymap.find( somekey ) // here happen bad things
    > if( it != mymap.end() )
    > {
    > // do something useful with it->second
    > }
    > else
    > {
    > // do nothing
    > }
    >
    > I traced a strange program crash (unhandled exception on windows,
    > something similar on Solaris) down to happen at the second line of
    > the code given above which contains the find(). After some
    > investigation I found out that it happens when the map is empty.
    >
    > This behaviour could be reproduced on Windows 2k and XP using MSVC 6.0
    > and 7.0 and finally also on Solaris unsing the Sun CC 5.4.
    >
    > I read the documentation as well as books and internet resources but I
    > could not find a single statement that on an empty map a find() must
    > not be performed.
    >
    > Could anybody please give me some information whether this is correct
    > or at least well known behaviour? Did I miss something? I do not like
    > the idea of preceeding all "critical" find() with some statement like
    > if( ! mymap.empty() )...


    This is not correct. It is perfectly permissible to use find with an
    empty map. The bug must be elsewhere.

    Bob
    Bob Bell, Oct 10, 2003
    #4
  5. Hello!

    > > std::map< int, MyClass*> mymap;
    > >
    > > it = mymap.find( somekey ) // here happen bad things
    > > if( it != mymap.end() )
    > > {
    > > // do something useful with it->second
    > > }
    > > else
    > > {
    > > // do nothing
    > > }
    > >
    > > I traced a strange program crash (unhandled exception on windows,
    > > something similar on Solaris) down to happen at the second line of
    > > the code given above which contains the find(). After some
    > > investigation I found out that it happens when the map is empty.


    I stepped through some STL code and found out that the cause is a
    simple dereferenced NULL pointer within the STL... In short and
    simplified words: find is looking for a node close neighbored to the
    given key, which results in a NULL pointer because the map is empty,
    and then the STL wants to access the parent of this node by
    dereferencing the pointer.

    So the crash can only be avoided by not doing "find" on empty maps...


    > > This behaviour could be reproduced on Windows 2k and XP using MSVC 6.0
    > > and 7.0 and finally also on Solaris unsing the Sun CC 5.4.


    The STL I stepped through was that contained with MSVC 7.0, but as
    already mentioned, the same behaviour I experienced also with MSVC 6.0
    and Sun CC 5.4.

    Since several library implementations seem to share this particular
    characteristic I would have expeted that it is well known or at least
    documented somewhere but concluding from he postings here I think it
    is not... :-(

    > This is not correct. It is perfectly permissible to use find with an
    > empty map. The bug must be elsewhere.
    >
    > Bob


    I dare say I would prefer the bug being elsewhere, because this
    behaviour is annyoing for me. Would you have a suggestion what bug
    outside the STL code could create the behaviour described above?
    Thanks!

    (Also thanks to all others who replied!)

    Matthias
    Matthias Hildebrand, Oct 13, 2003
    #5
  6. Matthias Hildebrand

    krogers

    Joined:
    Mar 20, 2012
    Messages:
    1
    Well this is frustrating, I'm experiencing the same issue with MSVS 2010
    krogers, Mar 20, 2012
    #6
    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. Peter Jansson
    Replies:
    5
    Views:
    6,294
    Ivan Vecerina
    Mar 17, 2005
  2. Replies:
    1
    Views:
    416
    red floyd
    Dec 21, 2008
  3. Thomas J. Gritzan
    Replies:
    6
    Views:
    1,012
    James Kanze
    Dec 22, 2008
  4. James Kanze
    Replies:
    0
    Views:
    1,993
    James Kanze
    Dec 21, 2008
  5. ~~~ .NET Ed ~~~

    Page button throws exception even when handler is empty

    ~~~ .NET Ed ~~~, Apr 4, 2005, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    138
    ~~~ .NET Ed ~~~
    Apr 4, 2005
Loading...

Share This Page