On Mon, 19 Apr 1999, Jim Schram wrote:

> >    tmpChar = *ptr;  // *ptr may be NULL
> >
> >    // always generates run-time 
> >    if(tmpChar != ':' && tmpChar != ';')  { // some code }
> >
> >    // never generates run-time error
> >    if(*ptr != ':' && *ptr != ';')  { // some code }
> 
> Hmmm...
> 
> Are you absolutely certain the error occurs on the first if-statement and NOT on the 
>tmpChar = *ptr assignment? If ptr is not valid going into the function that 
>assignment will fail (e.g. bus error) but depending on where ptr actually points, the 
>debugger might imply the wrong line number in its exception handler.

Yep! I have tried this many times by commenting out the first then the
second.  Only the first one causes a run-time error.
 
> Try switching the two if-statements around (and/or removing them entirely) and run 
>the test again.

The two 'if's are not in the code at the same time.  If the first 'if'
statement is in the code, it causes a run-time error.  If I remove the
first and replace it with the 2nd 'if' the code runs fine.

The way the code works is that if the 'if' statement is true, the routine
exits because I am done.  If it is not true the ptr is incremented and
the routine continues on.  I think this is where the problem occurs.  It
looks like the 'if' condition for the first example is FALSE and the
ptr is incremented past the end of the string, hence the error.

This routine is very simple.  I just make the assignment of tmpChar = char
pointed to by ptr.  Then test to see what the char was.  End if ':' or
';'.

Anyway, it works with the 2nd version so I may just drop the whole thing
but it bugs me why one works and one does not.

----------------------------------------------------
Shoot-to-Win

Protect the 2nd Amendment
----------------------------------------------------

Reply via email to