0

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.

J.N.
  • 8,203
  • 3
  • 29
  • 39
Kiran
  • 747
  • 2
  • 10
  • 35
  • Please show the code. Code is worth a thousand words. – Jesse Good Sep 05 '13 at 10:43
  • 1
    You could have some undefined behaviour. Could you show the declaration of `v`, and also how you fill its elements? – juanchopanza Sep 05 '13 at 10:43
  • Can you add the line related to the comment "/* v1 is vector defined before in the code. v[0] and v[1] are set to "Hello"*/" – doctorlove Sep 05 '13 at 10:45
  • the code is needed. You cannot post a question like //everything is perfect here cout << a << endl;//trying to print the results of perfect code //I have some weird behaviour here – Stefano Falasca Sep 05 '13 at 10:46
  • @juanchopanza I have used push_back function in vectors to store the elements. – Kiran Sep 05 '13 at 11:01
  • Are v1 and v2 always the same size? – Neil Kirk Sep 05 '13 at 11:12
  • 1
    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 05 '13 at 11:15
  • @NeilKirk Yes v1 and v2 are of same size. – Kiran Sep 05 '13 at 11:17
  • 1
    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 05 '13 at 11:17
  • @doctorlove What is SSO COW? – Neil Kirk Sep 05 '13 at 11:18
  • small string optimisation v. copy on write. But then I'd expect it to be ok on linux and fail on windows. – doctorlove Sep 05 '13 at 11:22
  • @BasileStarynkevitch: When I do that, warnings came about comparing signed and unsigned integer expressions in the comparison operation in for and if loops. – Kiran Sep 05 '13 at 11:23
  • 1
    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 05 '13 at 11:26
  • 1
    Then, correct your code till no warnings are given with `g++ -Wall -Wextra -g`, then use `gdb` to run your code for debugging... – Basile Starynkevitch Sep 05 '13 at 11:27
  • Ok. Thank you. I will do that. But can you explain why same code worked in windows with Code Blocks but not working with Ubuntu? – Kiran Sep 05 '13 at 11:28
  • 1
    Best guess: You have a bug that windows doesn't trigger. – dornhege Sep 05 '13 at 11:29
  • 1
    You probably have [undefined behavior](http://en.wikipedia.org/wiki/Undefined_behavior), and this can be bad enough to make you incorrectly think that your code is working sometimes. – Basile Starynkevitch Sep 05 '13 at 11:31

2 Answers2

0

The problem has to be in:

r1->GetSchema().GetAttrTypes();

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

Paul Evans
  • 27,315
  • 3
  • 37
  • 54
0

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.

Kiran
  • 747
  • 2
  • 10
  • 35
  • 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 http://stackoverflow.com/questions/8960055/getline-to-string-copies-newline-as-well – Travis Pessetto Sep 05 '13 at 14:44