473,406 Members | 2,894 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,406 software developers and data experts.

Odd map::find behavior on 64-bit windows

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 release build relying on printf of .size ()). So I would expect to have .find return .end () that is nothing. Now my map is a map of ptrs so it uses its own comparator function, and the thing is I see the comparator called for the data i am looking for as well as a NIL ptr. This only happens in release build not debug, so strikes me as odd.

My map is defined as:

Expand|Select|Wrap|Line Numbers
  1. typedef std::map<MyDataKey*, MyInfo*, MyLessThanFunctor> My_Map; 
and my find code:

Expand|Select|Wrap|Line Numbers
  1. My_Map::iterator find_itr = m_my_map.find (key_ptr);
I verified the key_ptr address is one of the items in my comparator, but when the map size is 0 why is the comparator even called? Where is this NIL coming from and why only in release builds? Thanks for the help.
Jul 12 '07 #1
6 2413
weaknessforcats
9,208 Expert Mod 8TB
Obviously ,there is a difference between the 32-bit and the 64-bit templates.

Do you have the latest set of templates?? Dinkumware always seems to have fixes beyond the Visual Studio release.
Jul 12 '07 #2
This was first recorded using .NET 2003 compiler and includes. However to make sure I went with .NET 2005 compiler for this latest version and is breaking. I have been using google to try and track down the issue (can't be the only one seeing this if its a MS problem), but no luck. Can't use any custom versions here as this is for work and we must work with the standard .NET framework. I am also going o try as a test is creating another map and seeing what happens. A collegue also mentioned allocating large arrays on either side of my map allocation and then checking the contents of them to see if somebody is overwriting the memory and messing things up. Might be worth a try.

Also for further background this code works on Linux both 32 and 64 bit both debug and non-debug builds. So the evidence is mounting that there is something wacky on Windows 64-bit release builds.
Jul 12 '07 #3
Followup after a long day of debugging. The map in question was a class member of my object. I had some map::insert and map::find calls in methods defined in the header file. Once I moved these and all constructor/destructor code to the cpp files it compiled and ran fine. So it seems .NET 2003/.NET 2005 have problems when compiling in release mode with code in the header files. Works on every other build platform, so I'll chalk this up to an MS bug in the compiler.
Jul 12 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
So it seems .NET 2003/.NET 2005 have problems when compiling in release mode with code in the header files.
There should never be code in a header file. Header files are for declarations and .cpp files for definitions. Code in a header file will get you redefinition errors unless it has internal linkage, which means you need to use static, which is deprecated in C++, etc, etc... and it makes your footprint bigger.

By declaration I mean that no memory is allocated. No variables and no functions.
Jul 13 '07 #5
We do this all of the time for inline functions. Otherwise the compiler complains that the code is unavailable. We also only do it in methods of a class, which is supported by C++. If I tried this with actual functions outside the context of a class, you would be right there would be compile errors (same as if I declared globals in a header, multiple copies). But putting code in the methods is perfectly fine. Just not on Windows 64-bit Release builds apparently.
Jul 13 '07 #6
weaknessforcats
9,208 Expert Mod 8TB
We do this all of the time for inline functions. Otherwise the compiler complains that the code is unavailable. We also only do it in methods of a class, which is supported by C++.
Inline functions are not function definitions. They are used by the compiler to copy inline where you call them, Functions inside classes are also inline functions. They, too, are copied as needed.

A function definition requires memory be allocated. An inline function allocates no memory so it is really more like a macro and, therefor, suitable, for a header file.

As to your problem, I did try your code on 32-bit Windows and there were no calls to the comparator. I tend to agree with you that it is an MS 64-bit compiler bug. You should report it.
Jul 14 '07 #7

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

Similar topics

4
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
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...
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...
3
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...
13
by: Peteroid | last post by:
Why does reading a member of a std::map not considered const? For example: class My_Class { int Get_Map_Value( int index ) const // ** error ** not considered const!!! { return m_Map ; //...
3
by: shaun | last post by:
I'm doing some work with const maps of various types and I find that I'm writing the same few lines of code over and over: (having declared and initialized a map<myTypeA, myTypeB> myMap) ...
5
by: subaruwrx88011 | last post by:
I am very new to c++ and very new to maps. Below is the source of my program. The output is very strange. Am I inserting correctly into the map? Any help will be greatly appreciated. #include...
17
by: devmentee | last post by:
Hello, I am trying to create a map/dictionary where the type of key is known ie std::string, but the value could be of any built in type. ie. int, double etc. (something along the lines of...
3
by: maheshr22 | last post by:
Hey Heres what i want to do: I want to create a map which has <string, vector<vector<int>>>. The string is always unique. What i need to do is, as i read each string from a file, i compare it...
12
by: Angus | last post by:
I am writing a class as a wrapper around a std::map. The class is: template<class TKey, class TValue> class CGeneralMap : protected map<TKey, TValue> At the moment I have basic functions like...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...
0
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...

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.