Click here to Skip to main content
15,892,575 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
The following Code fragment still baffles me. (It is part of a scanner, and I show the rest of the loop just as contex.
GetChar(char& ch)returns -1 at EOF, FALSE on Error,TRUE on success.
C++
while((bRes=GetChar(ch))==TRUE)

continues even when GetChar returns -1 (EOF).
I've been staring at this for the last few hours.





C++
// Get the Char Already Scanned as First Char of the Token
CString Tmp=Tok.m_sTokenString;

// Now keep Scanning until EOF or Non Lexical Char
while((bRes=GetChar(ch))==TRUE){
    TOKEN_CHAR tc=ClassifyTokenChar(ch);
    if((tc==TC_LEXICAL)||(tc==TC_NUMERICAL)){
        Tmp+=ch;
        continue;
    }
    else{
        UnGetChar(ch);
        break;
    }
}
Posted
Comments
Peter_in_2780 9-Nov-10 23:09pm    
What is the type of the variable bRes? Look for implicit conversions.
SnowHow 10-Nov-10 8:40am    
Are you never reaching your break condition? GetChar might not actually be working as specified as well as ClassifyTokenChar(ch) . What is ch anyhow what data type are you pulling from are you sure there is an EOF in ch?

How did you define the GetChar(ch),what's the return value's type~~~
 
Share this answer
 
Well, Resolved.

The Issue arrived in class CScanner, which is base class to CParser, which is Base Class to CDbScriptParser. An UnInitialised Var (pointer)in CParser clobbered the Stack To, by Great Coincidence set the value in the While Loop from -1 to 1.
Accept my apologies, I hope it will never happen any of the contributors to this post! (I also know that this is a veign hope for most of us). Beware all, when such coincidence happens, you will think you're brain is on fire! Logic does not seem to apply anymore. And Yes, I Should have known, in Restrospect, that this was a Stack Related Issue. Sometimes one should be allowed to Panick.

The way I found it, was by single stepping through the lines of generated assembler code.

Moral of the Event: It is helpfull to have a reading knowledge of the Target Processors Assembly Language.

Thanks to All!


Bram.
 
Share this answer
 
Try to return FALSE on EOF.
FALSE is 0.
TRUE is not 0.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900