Bug in 3.3.1 compiler libraries?

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

The non-dinkum are based on the gcc lib you should be able to find the source on the web. As for Dinkum I don’t beleive they are available.

I tried it on Linux with gcc 3.3.2 and it also showed not equal. I would check the libstdc++ reference to make sure you are calling it correctly…

Colin,

If your seeing the same behavior under Linux with GCC 3.3.2 then it would suggest that 3.3.1 under QNX is correct and what’s broken is the 2.95.3 compiler libraries (or rather their interpretation of how compare works).

I think to get the result I want (equal strings) under 3.3.1 that I will have to use a different compare function:

int compare(size_t pos, size_t n, const String S, size_t pos, size_t n2) which allows you to select the start and end points of both strings to do the comparison.

which would work out from the test case to be:

val = name.compare(0, (s_volumeString.length()-1), s_volumeString, 0, (s_volumeString.length()-1));

and indeed this does give what I am looking for (basically strncmp).

Tim