<vector> code, perfect on vc++6, broken on g++3.2.2

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ahmad

    <vector> code, perfect on vc++6, broken on g++3.2.2

    Hi all,

    I have written a simple c++ app, as I'm still learning c++. The
    thing works flawlessly on VC++6, but just doesn't work on g++. The
    transliterate function which causes the problem is supposed to search
    for some characters (2 or 1) and replace them with their equivalent
    Arabic characters from an array vector<string>. I have been debugging
    it on Linux for like 6 hours, and it's really boring that it works on
    vc++.

    Anyway, tons of thank yous to anyone that can help me with this one:
    PS: I noticed:
    arabic.size() returns 0??
    if I replace it with a number like 6, it just crashes?!
    declarations
    ------------
    std::vector<std ::string> arabic; //Array for arabic letters
    std::vector<std ::string> english; //Array for english letters
    std::vector<std ::string> comments; //Array for comments (ignored)
    code
    --------
    std::string arabize::transl iterate(std::st ring user_ip){
    std::cout << "In arabize Transliterator" << std::endl;
    std::string CharOrTwo;
    std::string text_out; //Holds the Arabic processed text
    bool MatchFound=fals e;

    if (user_ip.length () < 1) { std::cout << "Write some text first!" <<
    std::endl; return "";}
    for(int i=0; i < user_ip.length( ) ; ++i){
    //Loops over all input characters

    if (i < (user_ip.length ()-1)){ //If not at last char
    CharOrTwo = user_ip.at(i);
    CharOrTwo += user_ip.at(i+1) ; //Take next 2 chars
    for(int ctr = 0; ctr < arabic.size(); ctr++) //Problems here
    if (CharOrTwo == english[ctr] ){ //Are they in our dictionary?
    MatchFound=true ;
    text_out += arabic[ctr]; //Put their Arabic equivalent
    }
    if (MatchFound) {MatchFound=fal se; i++;continue;}//i++ bec we
    worked on 2 chars
    }
    //Next 2 chars didn't match, or we're at last char.
    //So, let's match this char we are at
    CharOrTwo = user_ip.at(i);
    for(int ctr = 0; ctr < arabic.size(); ctr++) //Again is it in
    dictionary
    if (CharOrTwo == english[ctr] ){
    MatchFound=true ;
    text_out += arabic[ctr]; //Put their Arabic equivalent
    }
    if (MatchFound) {MatchFound=fal se;continue;}


    }//Done iterating over all input chars
    #ifdef _DEBUG
    std::cout << "Input:" << user_ip << std::endl <<
    "Output:" << text_out << std::endl;
    #endif
    return text_out;
    }
  • Aggro

    #2
    Re: &lt;vector&g t; code, perfect on vc++6, broken on g++3.2.2

    Ahmad wrote:
    [color=blue]
    > I have written a simple c++ app, as I'm still learning c++. The
    > thing works flawlessly on VC++6, but just doesn't work on g++. The
    > transliterate function which causes the problem is supposed to search
    > for some characters (2 or 1) and replace them with their equivalent
    > Arabic characters from an array vector<string>. I have been debugging
    > it on Linux for like 6 hours, and it's really boring that it works on
    > vc++.[/color]

    Please follow the guidelines from the faq:

    [5.8] How do I post a question about code that doesn't work correctly?


    Comment

    • Ahmad

      #3
      Re: &lt;vector&g t; code, perfect on vc++6, broken on g++3.2.2

      OK, sorry if I was not very clear. Anyway, after playing with gdb, I
      think the problem is clearer (though still strange)

      CharOrTwo += user_ip.at(i+1) ; //Take next 2 chars
      for(int ctr = 0; ctr < arabic.size(); ctr++) //Problems here
      if (CharOrTwo == english[ctr] ){ //Are they in our dictionary?

      After the for loop, ctr has strange values of 100,000+ though it
      should be from 0 to 5??
      any help


      Aggro <spammerdream@y ahoo.com> wrote in message news:<s882c.492 $kA1.251@read3. inet.fi>...[color=blue]
      > Ahmad wrote:
      >[color=green]
      > > I have written a simple c++ app, as I'm still learning c++. The
      > > thing works flawlessly on VC++6, but just doesn't work on g++. The
      > > transliterate function which causes the problem is supposed to search
      > > for some characters (2 or 1) and replace them with their equivalent
      > > Arabic characters from an array vector<string>. I have been debugging
      > > it on Linux for like 6 hours, and it's really boring that it works on
      > > vc++.[/color]
      >
      > Please follow the guidelines from the faq:
      >
      > [5.8] How do I post a question about code that doesn't work correctly?
      >
      > http://www.parashift.com/c++-faq-lit...t.html#faq-5.8[/color]

      Comment

      • Leor Zolman

        #4
        Re: &lt;vector&g t; code, perfect on vc++6, broken on g++3.2.2

        On 6 Mar 2004 07:58:06 -0800, eng_ak@link.net (Ahmad) wrote:
        [color=blue]
        >OK, sorry if I was not very clear. Anyway, after playing with gdb, I
        >think the problem is clearer (though still strange)
        >
        >CharOrTwo += user_ip.at(i+1) ; //Take next 2 chars
        >for(int ctr = 0; ctr < arabic.size(); ctr++) //Problems here
        >if (CharOrTwo == english[ctr] ){ //Are they in our dictionary?
        >
        >After the for loop, ctr has strange values of 100,000+ though it
        >should be from 0 to 5??
        >any help
        >[/color]

        You haven't posted all your code, so I can't tell for sure, but I suspect
        you may be getting bitten by the "scope of a variable defined in a for
        loop" issue.

        In VC6, saying something like:

        for (int i = 0; i < whatever; ++i)
        {
        // whatever
        }

        is the same as saying:

        int i;
        for (i = 0; i < whatever; ++i)
        { // whatever
        }

        IOW, i remains in scope after the end of the loop. gcc probably does it
        /right/, which is to say, the first version above is "as if" you wrote:

        {
        int i;
        for (i = 0; i < whatever; ++i)
        { // whatever
        }
        }

        So perhaps you've got a version of your loop variable, "ctr", visible at
        the scope outside of the for loop? A global perchance? Just grasping at
        straws here...
        -leor

        Leor Zolman
        BD Software
        leor@bdsoft.com
        www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
        C++ users: Download BD Software's free STL Error Message
        Decryptor at www.bdsoft.com/tools/stlfilt.html

        Comment

        • Paul

          #5
          Re: &lt;vector&g t; code, perfect on vc++6, broken on g++3.2.2

          "Ahmad" <eng_ak@link.ne t> wrote in message
          news:3014031e.0 403060758.719f3 7c@posting.goog le.com...[color=blue]
          > OK, sorry if I was not very clear. Anyway, after playing with gdb, I
          > think the problem is clearer (though still strange)
          >
          > CharOrTwo += user_ip.at(i+1) ; //Take next 2 chars
          > for(int ctr = 0; ctr < arabic.size(); ctr++) //Problems here
          > if (CharOrTwo == english[ctr] ){ //Are they in our dictionary?
          >
          > After the for loop, ctr has strange values of 100,000+ though it
          > should be from 0 to 5??
          > any help
          >[/color]

          I guess you went ahead and ignored the posting guidelines. Please post a
          *complete*, *compilable*, example that demonstrates the problem.
          [color=blue]
          > have been debugging
          > it on Linux for like 6 hours, and it's really boring that it
          > works on vc++.[/color]

          This really doesn't mean anything, that it "works on vc++". There is a
          whole bunch of invalid code that I can come up that "works on vc++", but
          doesn't take away from the fact that the code could exhibit undefined
          behavior when compiled with another compiler.

          I see nowhere in the code where you call push_back(), resize(), or construct
          the vector objects that would appropriately size them.

          #include <vector>
          int main()
          {
          std::vector<int > IV;
          IV[0] = 10; // Illegal access

          std::vector<int > IV2;
          IV2.push_back(1 0); //OK
          }

          IV has no element 0, since no room for element 0 was created, so you have an
          illegal memory access.

          You are just lucky that it worked in Visual C++ *if* the code you posted is
          all you are doing with the vector. Also, how do you know it is a problem
          with vector, and not something else you are doing in the program that may
          have corrupted memory?

          This is why you should post *minimal*, *compilable* code that duplicates the
          problem.

          Paul


          Comment

          • John Harrison

            #6
            Re: &lt;vector&g t; code, perfect on vc++6, broken on g++3.2.2


            "Ahmad" <eng_ak@link.ne t> wrote in message
            news:3014031e.0 403060758.719f3 7c@posting.goog le.com...[color=blue]
            > OK, sorry if I was not very clear. Anyway, after playing with gdb, I
            > think the problem is clearer (though still strange)
            >
            > CharOrTwo += user_ip.at(i+1) ; //Take next 2 chars
            > for(int ctr = 0; ctr < arabic.size(); ctr++) //Problems here
            > if (CharOrTwo == english[ctr] ){ //Are they in our dictionary?
            >
            > After the for loop, ctr has strange values of 100,000+ though it
            > should be from 0 to 5??
            > any help
            >[/color]

            No still not very clear, please follow the guidelines in the FAQ



            You will get fast accurate help if you follow these guidelines, if you don't
            you will get guesses at best.

            john


            Comment

            Working...