473,219 Members | 1,528 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,219 software developers and data experts.

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

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
Jul 19 '05 #1
4 26141
"Matthias Hildebrand" <hi********@uni-kassel.de> 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

Jul 19 '05 #2
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?

Jul 19 '05 #3
hi********@uni-kassel.de (Matthias Hildebrand) wrote in message news:<65**************************@posting.google. com>...
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
Jul 19 '05 #4
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
Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
by: Duane Hebert | last post by:
2 questions: Given a map defined as std::map<int,string> stringmap; //How do you access the data_type (string) via an iterator? std::string spoo("doh");
3
by: Manuel Maria Diaz Gomez | last post by:
Hi everybody, This should be trivial, but I just can't see it. The following statement in a method doesn't work for me: (there's only one entry in myMap) map<const char*, int>::const_iterator...
2
by: ash | last post by:
Hi, I'm getting started with STL, and am stuck at creating a map container. I checked one of the texts and found a code in there. To make it simple, i wrote the following: #include...
44
by: jmoy | last post by:
I am a C programmer graduating to C++. As an exercise I wrote a program to count the number of times that different words occur in a text file. Though a hash table might have been a better choice,...
1
by: Saeed Amrollahi | last post by:
Dear All C++ Programmers Hello I am Saeed Amrollahi. I am a software engineer in Tehran Sewerage Company. I try to use std::map and map::find member function. I use Visual Studio .NET. my...
13
by: jstanforth | last post by:
This is probably a very obvious question, but I'm not clear on what operators need to be implemented for std::map.find() to work. For example, I have a class MyString that wraps std::string, and...
2
by: mergaite_lietuvaite | last post by:
Hi, I try to use map. My code is very easy, but there are some errors.... (by compiling). Plaese, could you say, what I do wrong.. struct less_array { bool operator()(const FArray p1, const...
6
by: Benny the Guard | last post by:
Been working on a project that compiles, links, and runs fine on 32-bit windows. Now trying to migrate to 64-bit windows. On debug build it works fine. However, on release build the std::map::find...
3
by: digz | last post by:
This is a very simplified version of something I am trying to understand. The State object holds the strings and maps , and I pass a reference to State to the process Function which manipulates it...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.