Found Nasty Bug in GCC 4.1.2 on CentOS 5
Friday, February 29th, 2008I was working on my latest price feeder today and got word that I wasn't sending enough information to the receiving system for it to make proper use of the data. Basically, I needed to get more data about the instruments from this system and then feed it back to the system with each price for each instrument. Problem was, that really made the way I was storing data for each instrument a royal pain and I needed to go from a simple STL data structure to a class that will hold all the data for each instrument and then put these instances into a data structure for efficient access.
What I did was something a lot like this test code:
/* * This trys to reproduce the bug I was seeing in the initialization * of the CKString while in a CKVector in a std::map. */ #include <iostream> #include <map> #include "CKVector.h" #include "CKString.h" typedef struct tuple_t { CKString one; CKString two; CKString three; // constructors tuple_t() : one(), two(), three() { } tuple_t( const CKString & aOne, const CKString & aTwo, const CKString & aThree ) : one(aOne), two(aTwo), three(aThree) { } tuple_t( const tuple_t & anOther ) : one(), two(), three() { *this = anOther; } virtual ~tuple_t() { } tuple_t & operator=( const tuple_t & anOther ) { one = anOther.one; two = anOther.two; three = anOther.three; return *this; } bool operator==( const tuple_t & anOther ) { bool equal = true; if ((one != anOther.one) || (two != anOther.two) || (three != anOther.three)) { equal = false; } return equal; } bool operator!=( const tuple_t & anOther ) { return !operator==(anOther); } CKString toString() const { CKString retval("[one="); retval.append(one).append(", two=").append(two). append(", three=").append(three).append("]"); return retval; } } tuple; typedef CKVector<tuple> TList; typedef std::map<CKString, TList> TListMap; int main(int argc, char *argv[]) { // make the map - no entries TListMap myMap; // this is the first key we'll be using CKString myKey("key"); // create the first map entry TList & list = myMap[myKey]; // create a tuple to place on the list tuple t("a", "b", "c"); std::cout << "tuple = " << t.toString() << std::endl; // put the tuple on the list std::cout << "list has " << list.size() << " elems with capacity of " << list.capacity() << std::endl; if (!list.contains(t)) { std::cout << "tuple not in list, adding..." << std::endl; list.addToEnd(t); } // print out the map std::cout << "here's the map now: "; TListMap::iterator mi; for (mi = myMap.begin(); mi != myMap.end(); ++mi) { std::cout << mi->first << " = ["; for (int i = 0; i < mi->second.size(); ++i) { if (i != 0) { std::cout << ", "; } std::cout << mi->second[i].toString(); } std::cout << "]" << std::endl; } }
Lines 10-76 simply creates the object that is going to hold the instrument data - in this case, three pieces of data. Line 78 creates a type that is a simple vector of these tuples. Line 79 creates an STL map where the key is a string and the value is a vector of these tuples. Seems like a reasonable data structure, given that I'm basing this all on CKit.
In the main method, I'm basically creating a map of these string-list pairs, then creating a tuple and seeing if the new key isn't there - it's not, and then adding the tuple to the end of the list. This all works fine on Mac OS X 10.5.2 and GCC 4.0.1 (Xcode 3.0). But on RHEL 5 (CentOS 5) with GCC 4.1.2 we get an entirely different result.
At line 97 we get a very spectacular core dump. The gdp session is basically showing me that the initial value of one in the existing allocated storage in the vector for the first element is not being properly initialized. This initialization is done properly on OS X, but on RHEL5 it's not. When I change the typedefs to say something like this:
typedef CKVector<tuple> TList; typedef std::map<CKString, TList*> TListMap;
thus making it necessary for me to new a TList and place that into the TListMap, and then delete it when I'm done, then things work just fine and the initialization is properly done.
I'm not that big of a stickler for compiler bugs - they happen, but so rarely that it's not worth getting all bent out of shape over one that's so easily worked-around. But I'll admit, it's a little frustrating to think that it's your fault for several hours only to find out that it's not, that at least a part of me wants to send in a bug report, but I probably won't. Just glad I got it figured out.