Take the 2-minute tour ×

I have to compare two strings which are stored in a vector. Comparison works fine in Windows which uses mingw-g++ compiler(version 4.4.1).

But when I try to do the same in Ubuntu which is running g++ version 4.7.2 I am getting weird problems. I listed them below:

  1. When I try to print the elements individually, they are giving correct output and both strings are same.

  2. But when I try to compare them using == operator or strcmp() it is saying that they are not equal even though they are same.

  3. When I try to print the elements which are compared above some string is replacing first string's value. The code is given below. Ideally the if loop shouldn't be executed, but it is still executing and printing some garbage value instead of v1[i].

vector<string> v1 = r1->GetSchema().GetAttrTypes();
vector<string> v2 = r2->GetSchema().GetAttrTypes();
for(i=0; i<v2.size();i++)
    if(v1[i] != v2[i])
        cout << v1[i] << " " << v2[i] << " awdsd" << endl;

I don't know what to search for these kind of errors. I am taking strings from same file and storing in vectors in both Windows and Ubuntu.

EDIT: I am attaching part of the code here. The function right side returns vector of strings. I can't paste above classe's code as it is very big code.

share|improve this question
You could have some undefined behaviour. Could you show the declaration of v, and also how you fill its elements? –  juanchopanza Sep 5 '13 at 10:43
I see no push_back –  doctorlove Sep 5 '13 at 11:06
Did you enable all warnings and debugging info for GCC (i.e. g++ -Wall -g)? Did you correct the code till no warnings are given? Did you use the gdb debugger? –  Basile Starynkevitch Sep 5 '13 at 11:15
WIndows v. linux string differences are often due to SSO v COW. The code populating the vectors is probably buggy and you haven't shown it in full. –  doctorlove Sep 5 '13 at 11:17
The strings probably really differ on Linux. That means that the code that actually generates the strings has a problem and produces wrong results on Linux. But of course that's just a theory. –  Nikos C. Sep 5 '13 at 11:26

2 Answers 2

The problem has to be in:


This function must be mucking about with how it's std::string's are created. Can you show us the code?

share|improve this answer

Well, everything was just fine, except file from which I am reading input string. Since I created that file on windows, it's End of line is different(\r\n) where as in Ubuntu it is (\n).

In Ubuntu I had to convert the text file I am reading input Unix Mode. After that everything is fine.

I don't know how I missed this small point.

Thank you for your input.

share|improve this answer
Why not just trim off the end lines in your code? That way it won't care if the file is Windows or some Unix variant. See stackoverflow.com/questions/8960055/… –  Travis Pessetto Sep 5 '13 at 14:44

Your Answer


