473,729 Members | 2,149 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Standard Template Library map:find() returns garbage

2 New Member
I have run into a problem that I cannot seem to figure out regarding a method call to STL map::find(). When the key value can't be found in the map (this is an expected scenario), it returns end() that has _MyValue = unitialized memory (it appears). Thus, the returned iterator is garbage and an exception is thrown eventually. I thought that find() returned the location succeeding the last element in the map if a match was not found. It seems to be returning end(). The map looks like this:

Expand|Select|Wrap|Line Numbers
  1. typedef list< CpProts > SRCP_PROTS_LIST;
  2. typedef map< long, SRCP_PROTS_LIST*, CpProtsCmp, CpProtsList2 >    SRCP_PROTS_LISTMAP;
where:
- CpProts is a class containing methods and fields
- class CpProtsCmp : public less< long > {};
- class CpProtsList : public allocator< CpProts > {};
- class CpProtsList2 : public allocator< std::list<CpPro ts,CpProtsList> *> {};

here is the code where the exception occurs:
Expand|Select|Wrap|Line Numbers
  1. DWORD imageType = 0x1000007F;
  2. SRCP_PROTS_LISTMAP m_ProtsListMap;
  3. SRCP_PROTS_LISTMAP::iterator listMapIt = m_ProtsListMap.find(imageType);
  4. SRCP_PROTS_LIST* pList = (*listMapIt).second;  <<-- 
  5.  
  6. listMapIt.ptr->second =  0xbaadf00d 
a little complicated but it has worked up until we changed from VS 6.0 to VS 2005 earlier this year. I've inspected the map once all of the values are added and everything looks like I would expect. Using a debug build, I've crawled through the MS code and it seems like the compares (lessthan) are working correctly except in the case where the key cannot be found in the map.

I am using Visual Studio 2005 on Windows XP SP2, Dell 670 workstation. I'm at the end of my rope so I am hopeful that someone out there can see what I can't see.

thanks,
Cyndy
Jul 10 '07 #1
2 2156
weaknessforcats
9,208 Recognized Expert Moderator Expert
DWORD imageType = 0x1000007F;
SRCP_PROTS_LIST MAP m_ProtsListMap;
SRCP_PROTS_LIST MAP::iterator listMapIt = m_ProtsListMap. find(imageType) ;
SRCP_PROTS_LIST * pList = (*listMapIt).se cond; <<--

listMapIt.ptr->second = 0xbaadf00d
When the find fails, listMapIt is m_ProtsListMap. end(). That means listMapIt is garbage.

You need to:
Expand|Select|Wrap|Line Numbers
  1. DWORD imageType = 0x1000007F;
  2. SRCP_PROTS_LISTMAP m_ProtsListMap;
  3. SRCP_PROTS_LISTMAP::iterator listMapIt = m_ProtsListMap.find(imageType);
  4. SRCP_PROTS_LIST* pList;
  5.  
  6. if (listMapIt != m_ProtsListMap.end())
  7. {
  8.      plist = (*listMapIt).second;  
  9. }
  10. else
  11. {
  12.      //whatever you do when it's not found.
  13.  
Jul 11 '07 #2
cyndykoobs
2 New Member
When the find fails, listMapIt is m_ProtsListMap. end(). That means listMapIt is garbage.

You need to:
Expand|Select|Wrap|Line Numbers
  1. DWORD imageType = 0x1000007F;
  2. SRCP_PROTS_LISTMAP m_ProtsListMap;
  3. SRCP_PROTS_LISTMAP::iterator listMapIt = m_ProtsListMap.find(imageType);
  4. SRCP_PROTS_LIST* pList;
  5.  
  6. if (listMapIt != m_ProtsListMap.end())
  7. {
  8.      plist = (*listMapIt).second;  
  9. }
  10. else
  11. {
  12.      //whatever you do when it's not found.
  13.  
Thanks "WeaknessForCat s" (I'm a cat lover so I appreciate your 'name'). What you suggested so makes sense and I will try and incorporate that into the logic somehow (this is not my code so I'm not exactly sure what the person needs to happen if it's not found). I am so curious why this used to work and doesn't now. No code changes were made in this file except to get rid of some compiler warnings for VS2005. I walked through the code using VS 6.0 and I get back the same result. However, the line of code
Expand|Select|Wrap|Line Numbers
  1. SRCP_PROTS_LIST* pList = (*listMapIt).second;
doesn't crash the application even when the iterator passed back is end(). Perhaps it's a case of several things being wrong that made it work right before.

anway, thanks again for taking the time to point out something that's pretty darn obvious now.
Jul 11 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
2440
by: F. Petitjean | last post by:
I have written a script to find the modules which export the largest number of names. The gc.getreferrers(*objs) function gives also an idea of the dependencies between the modules. The code (statsmod.py) : #!/usr/bin/env python # -*- coding: latin-1 -*- """
4
26405
by: Matthias Hildebrand | last post by:
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
3
7458
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 it = myMap.find(key); where key is a const char* passed by the class method.
3
3846
by: rudymoore | last post by:
I'm often find-ing the same string in a map<string,xxx> I want to pass a hint to map::find but was disappointed to find that only map::insert takes the hint. Why would map::insert take a hint but not map::find? Thanks, Rudy
1
4004
by: Jean Stax | last post by:
Hi ! I created a sample library project. In my second project I reference this library and make the following call, which returns "undefined value": Type myType = Type.GetType("SampleLib.MyClass"); My guess was that the function fails because my library isn't loaded yet.
3
2706
by: Ahmed Yasser | last post by:
Hi everybody, Can anyone tell how to call a standard "Find and Replace dialog box" while my application is at run time. Is there any embeded somewhere ?? or do i have to design one !! Thanks in advance.
4
1747
by: Ahmed Yasser | last post by:
Hi Everybody, can anyone help in sending code reference to build a simple find dialog box like the one we call by (ctrl+F). Thanks a lot.
6
2450
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 operation starts acting odd. My code simply does a lookup, if it fails it inserts the proper element (creating the data element is expensive so I only want to do it if needed and many times its not). In the lookup I see the size is 0 (since...
2
2049
by: dhirajgaherwar | last post by:
Hi everybody, Can anyone tell how to call a standard "Find and Replace dialog box" while my application (embeded word aplication) is at run time. Is there any embeded somewhere ?? or do i have to design one !! i am having a win application that i have created myself on VB. there is no word menu bar. i would like the user to call a standard Find dialog box (Ctrl+F) to jump to a specific line in the treeview for ease in searching and...
0
9426
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9200
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9142
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6016
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3238
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2162
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.