function doesn't return simply skip the return line

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • hirsh.dan@gmail.com

    function doesn't return simply skip the return line

    Hello to all, i have the following functions:

    string File::readLine( ){
    char ch;
    string str;
    ch = read();
    while(ch != LF && ch != CR && ch != -1){
    str.append(1,ch );
    ch = read();
    }
    if(ch == -1){
    cout << "this line could not be read" << endl;
    exit(1);
    }
    //take care of situation where the loop exited with LF but
    //on a system that has CR.
    int tmp = file->tellg();
    if(read() != CR){ //if no CR available on system, return the get
    pointer to it's correct location.
    file->seekg(tmp);
    }
    return str;
    }

    the function is executed correctly but in the end it skips the "return
    str" line,
    and tun flies with an error.
    debugging took me to a qt class moc_myclass.cpp (it's a qt project in
    eclipse)
    to a line where it says "_id -= 1;"
    inside a function called "int myclass::qt_met acall(...)"

    can anyone help me??????????
  • Victor Bazarov

    #2
    Re: function doesn't return simply skip the return line

    hirsh.dan@gmail .com wrote:
    Hello to all, i have the following functions:
    >
    string File::readLine( ){
    char ch;
    string str;
    ch = read();
    while(ch != LF && ch != CR && ch != -1){
    str.append(1,ch );
    ch = read();
    }
    if(ch == -1){
    cout << "this line could not be read" << endl;
    exit(1);
    }
    //take care of situation where the loop exited with LF but
    //on a system that has CR.
    int tmp = file->tellg();
    if(read() != CR){ //if no CR available on system, return the get
    pointer to it's correct location.
    file->seekg(tmp);
    }
    return str;
    }
    >
    the function is executed correctly but in the end it skips the "return
    str" line,
    and tun flies with an error.
    debugging took me to a qt class moc_myclass.cpp (it's a qt project in
    eclipse)
    to a line where it says "_id -= 1;"
    inside a function called "int myclass::qt_met acall(...)"
    >
    can anyone help me??????????
    Since 'qt' is not part of C++, you should consider asking in the
    newsgroup (or a web forum) dedicated to that. Apparently the code you
    posted cannot be verified without the rest of the stuff (which you did
    not post).

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask

    Comment

    • hirsh.dan@gmail.com

      #3
      Re: function doesn't return simply skip the return line

      On 21 יולי, 17:57, Victor Bazarov <v.Abaza...@com Acast.netwrote:
      hirsh....@gmail .com wrote:
      Hello to all, i have the following functions:
      >
      string File::readLine( ){
      char ch;
      string str;
      ch = read();
      while(ch != LF && ch != CR && ch != -1){
      str.append(1,ch );
      ch = read();
      }
      if(ch == -1){
      cout << "this line could not be read" << endl;
      exit(1);
      }
      //take care of situation where the loop exited with LF but
      //on a system that has CR.
      int tmp = file->tellg();
      if(read() != CR){ //if no CR available on system, return the get
      pointer to it's correct location.
      file->seekg(tmp);
      }
      return str;
      }
      >
      the function is executed correctly but in the end it skips the "return
      str" line,
      and tun flies with an error.
      debugging took me to a qt class moc_myclass.cpp (it's a qt project in
      eclipse)
      to a line where it says "_id -= 1;"
      inside a function called "int myclass::qt_met acall(...)"
      >
      can anyone help me??????????
      >
      Since 'qt' is not part of C++, you should consider asking in the
      newsgroup (or a web forum) dedicated to that. Apparently the code you
      posted cannot be verified without the rest of the stuff (which you did
      not post).
      >
      V
      --
      Please remove capital 'A's when replying by e-mail
      I do not respond to top-posted replies, please don't ask
      maybe, but it skips a regular c++ line of code,
      also if i put a cout before the call to return, it prints out (goes
      there) but just before
      return is supposed to execute, it flies
      don't you have any ideas why might that be happening,
      isn't my c++ code written well, what else should i post for you to get
      the whole picture
      ?

      Comment

      • Victor Bazarov

        #4
        Re: function doesn't return simply skip the return line

        hirsh.dan@gmail .com wrote:
        On 21 יולי, 17:57, Victor Bazarov <v.Abaza...@com Acast.netwrote:
        >hirsh....@gmai l.com wrote:
        >>Hello to all, i have the following functions:
        >>string File::readLine( ){
        >> char ch;
        >> string str;
        >> ch = read();
        >> while(ch != LF && ch != CR && ch != -1){
        >> str.append(1,ch );
        >> ch = read();
        >> }
        >> if(ch == -1){
        >> cout << "this line could not be read" << endl;
        >> exit(1);
        >> }
        >> //take care of situation where the loop exited with LF but
        >> //on a system that has CR.
        >> int tmp = file->tellg();
        >> if(read() != CR){ //if no CR available on system, return the get
        >>pointer to it's correct location.
        >> file->seekg(tmp);
        >> }
        >> return str;
        >>}
        >>the function is executed correctly but in the end it skips the "return
        >>str" line,
        >>and tun flies with an error.
        >>debugging took me to a qt class moc_myclass.cpp (it's a qt project in
        >>eclipse)
        >>to a line where it says "_id -= 1;"
        >>inside a function called "int myclass::qt_met acall(...)"
        >>can anyone help me??????????
        >Since 'qt' is not part of C++, you should consider asking in the
        >newsgroup (or a web forum) dedicated to that. Apparently the code you
        >posted cannot be verified without the rest of the stuff (which you did
        >not post).
        >>
        >V
        >--
        >Please remove capital 'A's when replying by e-mail
        >I do not respond to top-posted replies, please don't ask
        >
        maybe, but it skips a regular c++ line of code,
        How do you know? Did you step through it in the debugger? If the error
        is returned, it's possible that an exception is thrown and then caught
        by some invisible code. Qt runs some files through their own "meta
        compiler" or something, and who know what kind of code they insert into
        what you think is "regular c++"...
        also if i put a cout before the call to return, it prints out (goes
        there) but just before
        return is supposed to execute, it flies
        Examine the assembly.
        don't you have any ideas why might that be happening,
        Nope. Qt is not part of C++, and the sheer fact that you are using it
        can render your program incomprehensibl e from the "regular c++" POV.
        isn't my c++ code written well, what else should i post for you to get
        the whole picture
        ?
        Again, the code you posted is fine. However, you didn't post enough of
        it, for example, what's happening in the 'read()' function?

        V
        --
        Please remove capital 'A's when replying by e-mail
        I do not respond to top-posted replies, please don't ask

        Comment

        • hirsh.dan@gmail.com

          #5
          Re: function doesn't return simply skip the return line

          On 21 יולי, 21:49, Victor Bazarov <v.Abaza...@com Acast.netwrote:
          hirsh....@gmail .com wrote:
          On 21 יולי, 17:57, Victor Bazarov <v.Abaza...@com Acast.netwrote:
          hirsh....@gmail .com wrote:
          >Hello to all, i have the following functions:
          >string File::readLine( ){
          > char ch;
          > string str;
          > ch = read();
          > while(ch != LF && ch != CR && ch != -1){
          > str.append(1,ch );
          > ch = read();
          > }
          > if(ch == -1){
          > cout << "this line could not be read" << endl;
          > exit(1);
          > }
          > //take care of situation where the loop exited with LF but
          > //on a system that has CR.
          > int tmp = file->tellg();
          > if(read() != CR){ //if no CR available on system, return the get
          >pointer to it's correct location.
          > file->seekg(tmp);
          > }
          > return str;
          >}
          >the function is executed correctly but in the end it skips the "return
          >str" line,
          >and tun flies with an error.
          >debugging took me to a qt class moc_myclass.cpp (it's a qt project in
          >eclipse)
          >to a line where it says "_id -= 1;"
          >inside a function called "int myclass::qt_met acall(...)"
          >can anyone help me??????????
          Since 'qt' is not part of C++, you should consider asking in the
          newsgroup (or a web forum) dedicated to that. Apparently the code you
          posted cannot be verified without the rest of the stuff (which you did
          not post).
          >
          V
          --
          Please remove capital 'A's when replying by e-mail
          I do not respond to top-posted replies, please don't ask
          >
          maybe, but it skips a regular c++ line of code,
          >
          How do you know? Did you step through it in the debugger? If the error
          is returned, it's possible that an exception is thrown and then caught
          by some invisible code. Qt runs some files through their own "meta
          compiler" or something, and who know what kind of code they insert into
          what you think is "regular c++"...
          >
          also if i put a cout before the call to return, it prints out (goes
          there) but just before
          return is supposed to execute, it flies
          >
          Examine the assembly.
          >
          don't you have any ideas why might that be happening,
          >
          Nope. Qt is not part of C++, and the sheer fact that you are using it
          can render your program incomprehensibl e from the "regular c++" POV.
          >
          isn't my c++ code written well, what else should i post for you to get
          the whole picture
          ?
          >
          Again, the code you posted is fine. However, you didn't post enough of
          it, for example, what's happening in the 'read()' function?
          >
          V
          --
          Please remove capital 'A's when replying by e-mail
          I do not respond to top-posted replies, please don't ask
          well, thank you Victor,
          in my next post i shell reference your guidance on writing a thread
          that
          is more understood, and acceptable.
          have a good day.

          P.S
          i guess the capital A was for a new line, but i wasn't innuendoing
          anything
          next time I will write only letters.

          Comment

          • James Kanze

            #6
            Re: function doesn't return simply skip the return line

            On Jul 21, 4:50 pm, hirsh....@gmail .com wrote:
            Hello to all, i have the following functions:
            string File::readLine( ){
            char ch;
            string str;
            ch = read();
            while(ch != LF && ch != CR && ch != -1){
            Nothing to do with your problem, but there's no guarantee that a
            char can ever take on the value of -1, so you've definitely got
            something wrong here. I don't know a read() function which
            takes no arguments, so I can't be sure, but the usual idiom
            (from C) for this sort of thing is for functions which read a
            single character to return an int, with a value in the range
            0...UCHAR_MAX, or a negative value for EOF. But this supposes
            that the returned value is stored in an int, and not a char.
            str.append(1,ch );
            Rather than "str += ch"? Strange.
            ch = read();
            }
            if(ch == -1){
            cout << "this line could not be read" << endl;
            exit(1);
            }
            //take care of situation where the loop exited with LF but
            //on a system that has CR.
            int tmp = file->tellg();
            Do you know what type ifstream::tellg returns? It isn't int.
            if(read() != CR){ //if no CR available on system, return the get
            pointer to it's correct location.
            file->seekg(tmp);
            }
            What's the relationship between file and where ever read()
            accesses. If read() is accessing file, and file is an istream,
            why not use file directly, e.g.:

            if ( file->peek() == CR ) {
            file->get() ;
            }
            return str;
            }
            the function is executed correctly but in the end it skips the "return
            str" line,
            and tun flies with an error.
            What does that mean? "Tun flies with an error." I don't
            understand it. If you reach the return statement, it will be
            executed. It may cause an exception to be thrown (because str
            must be copied, and copying a string may throw), but unless
            you're right at the limit in memory use, it's highly unlikely.

            Another possibility (very likely, IMHO) is that you've corrupted
            the free space arena sometime earlier, and you just happened to
            hit the problem when copying str.
            debugging took me to a qt class moc_myclass.cpp (it's a qt
            project in eclipse)
            to a line where it says "_id -= 1;"
            inside a function called "int myclass::qt_met acall(...)"
            I doubt that that's significant, but you never know. More
            likely something else is corrupt, which results in your stack
            being corrupted, which results in your returning someplace else
            than where you came from.

            --
            James Kanze (GABI Software) email:james.kan ze@gmail.com
            Conseils en informatique orientée objet/
            Beratung in objektorientier ter Datenverarbeitu ng
            9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

            Comment

            Working...