I think I have found a bug in the 3.3.1 libraries.
Here is a small test program I wrote to illustrate the problem.
#include
#include <stdio.h>
#include <stdlib.h>
#include
using namespace std;
const std::string s_volumeString = “Volume”;
int main()
{
std::string name = “Volume1”;
int val = 0;
val = name.compare(0, (s_volumeString.length()-1), s_volumeString);
if (val == 0)
{
cout << "Strings are equal" << endl;
}
else
{
cout << "Strings are not equal " << val << endl;
}
return 0;
}
Compile this using the 3.3.1 compiler are follows (or use Dinkum libs, makes no difference):
qcc -V3.3.1,gcc_ntox86_gpp test.cpp
Then run it and you’ll get ‘strings are not equal -1’ printed.
Now compile the same program (minus the different order in the ‘compare’ arguments) under 2.95.3.
#include
#include <stdio.h>
#include <stdlib.h>
#include
using namespace std;
const string s_volumeString = “Volume”;
int main()
{
string name = “Volume1”;
int val = 0;
val = name.compare(s_volumeString, 0, (s_volumeString.length()-1));
if (val == 0)
{
cout << "Strings are equal" << endl;
}
else
{
cout << "Strings are not equal " << val << endl;
}
return 0;
}
Compile this using the 2.95.3 compiler are follows:
qcc -V2.95.3,gcc_ntox86_gpp test.cpp
Then run it and you’ll get ‘strings are equal’ printed.
So, how did the compare function ‘break’ under 3.3.1? According to Stroustrup and other sources those strings ‘should’ be equal as it is essentially doing a strncmp of the number of characters specified.
In fact it gets weirder. If you remove the -1 from the length part then it works in 3.3.1 and the strings are equal. But if you for any reason compare anything other than the total length of the 2nd string to the first one then they are never equal.
Something is wrong in the libraries both Dinkum and non-Dinkum. Is it possible to see the c++ source code for the standard libraries anywhere for QNX? I only have the header files on my system.
Tim