function doesn't return simply skip the return line

Discussion in 'C++' started by hirsh.dan@gmail.com, Jul 21, 2008.

  1. Guest

    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_metacall(...)"

    can anyone help me??????????
     
    , Jul 21, 2008
    #1
    1. Advertising

  2. Guest

    On 21 יולי, 17:57, Victor Bazarov <> wrote:
    > 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_metacall(...)"

    >
    > > 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
    ?
     
    , Jul 21, 2008
    #2
    1. Advertising

  3. Guest

    On 21 יולי, 21:49, Victor Bazarov <> wrote:
    > wrote:
    > > On 21 יולי, 17:57, Victor Bazarov <> wrote:
    > >> 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_metacall(...)"
    > >>> 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 incomprehensible 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.
     
    , Jul 22, 2008
    #3
  4. a écrit :
    [snip]
    > 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_metacall(...)"
    >
    > can anyone help me??????????


    From Trolltech documentation:
    http://doc.trolltech.com/qq/qq16-dynamicqobject.html
    <<<
    When a signal is emitted, Qt uses qt_metacall() to invoke the slots
    connected to the signal. The first parameter, call, is then set to
    QMetaObject::InvokeMetaMethod. (The qt_metacall() function is also used
    for other types of access to the meta-object, such as setting or getting
    properties.)
    >>>


    --
    Michael
     
    Michael DOUBEZ, Jul 22, 2008
    #4
  5. James Kanze Guest

    On Jul 21, 4:50 pm, 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_metacall(...)"


    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:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Jul 22, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. frank
    Replies:
    0
    Views:
    339
    frank
    Jul 11, 2003
  2. michael
    Replies:
    2
    Views:
    260
    =?Utf-8?B?RGF2aWQgQ29lLCBNQ1A=?=
    Feb 23, 2004
  3. Replies:
    15
    Views:
    447
    tobiah
    Dec 18, 2006
  4. Tito Ciuro
    Replies:
    13
    Views:
    211
    Alex Young
    Oct 20, 2007
  5. PerlFAQ Server

    FAQ 7.4 How do I skip some return values?

    PerlFAQ Server, Mar 26, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    100
    PerlFAQ Server
    Mar 26, 2011
Loading...

Share This Page