C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28629
Number of posts: 94611

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
using strod() with strange results Posted by gonzilla on 11 Jan 2002 at 8:22 AM
Hi,

I am using strod() to convert a string - "1 QA" into an index and a label. So this would be an index of 1 and a label of QA. Here is the program.

int main (void)
{
int BUFSIZE = 100;
int i, Index;
FILE *Fp;
char Buffer[BUFSIZ];
char * P, * NewLine;

Fp = fopen("filename","r");
if (!Fp)
{
printf("Not able to open file!\n");
exit(1);
}
else
{
for (i=1; fgets(Buffer,BUFSIZ,Fp) ; i++)
{
Index = strtod(Buffer,&P);
if (P == Buffer)
{
printf("Line %d: missing index for label %s", i, Buffer);
continue;
}
if (Index < 0 || Index > 100)
{
printf("Line %d: Bad index %d for label %s",
i, Index, Buffer);
continue;
}
while (isspace(*P))
P++;
NewLine = strchr(P,'\n');
if (NewLine)
{
*NewLine = '\0';
}
printf("Label is %s index is %d\n", P, Index);
}
}
fclose(Fp);
}

The strange part is that it works fine as a stand alone program, but when used as part of an algorithm set for a system, strod() returns a bad index...

Line 1: Bad index -268450352 for label 1 QA

This is using the same exact same input file in both cases. Why would it work sometimes and not others?

Thanks.
Report
Re: using strod() with strange results Posted by Eric Tetz on 11 Jan 2002 at 10:45 AM
: Hi, :
: I am using strod() to convert a string - "1 QA" into an index and a label. So this would be an index of 1 and a label of QA.
[snipped - code]
: The strange part is that it works fine as a stand alone program, but when used as part of an algorithm set for a system, strod() returns a bad index...
:
: Line 1: Bad index -268450352 for label 1 QA
:
: This is using the same exact same input file in both cases. Why would it work sometimes and not others?

It wouldn't. It's getting different input. To find out why, you can either:

(a) Add printf statements to your program to show the state of variables during execution. For instance, right after reading a line from the file, and before parsing it, add the line:
printf("'%s'\n", Buffer);

To show what it is you're parsing.

(b) Step through the code in a debugger. This is by far the most powerful option. If you haven't learned to use a debugger yet, now would be a good time to start. This is an essential weapon in any programmer's arsenal.

Of course, there's option (d), which is to stare at the code really hard until you find the bug... but I assume you've already tried that. ;)

Cheers,
Eric

Report
Re: using strod() with strange results Posted by gonzilla on 11 Jan 2002 at 10:57 AM
: : Hi, :
: : I am using strod() to convert a string - "1 QA" into an index and a label. So this would be an index of 1 and a label of QA.
: [snipped - code]
: : The strange part is that it works fine as a stand alone program, but when used as part of an algorithm set for a system, strod() returns a bad index...
: :
: : Line 1: Bad index -268450352 for label 1 QA
: :
: : This is using the same exact same input file in both cases. Why would it work sometimes and not others?
:
: It wouldn't. It's getting different input. To find out why, you can either:
:
: (a) Add printf statements to your program to show the state of variables during execution. For instance, right after reading a line from the file, and before parsing it, add the line:
:
: printf("'%s'\n", Buffer);
: 

: To show what it is you're parsing.
:
: (b) Step through the code in a debugger. This is by far the most powerful option. If you haven't learned to use a debugger yet, now would be a good time to start. This is an essential weapon in any programmer's arsenal.
:
: Of course, there's option (d), which is to stare at the code really hard until you find the bug... but I assume you've already tried that. ;)
:
: Cheers,
: Eric
:
:
Hi,

I've tried all of the options except using the debugger. It doesn't get different input as far as I can see. I would love to use a debugger but this is on UNIX - I don't think our system has a deugger for it. I'll give it a try with a Windows compiler and see what happens...and then stare some more at it...

Thanks Eric.
Report
Re: using strod() with strange results Posted by Eric Tetz on 11 Jan 2002 at 11:33 AM
: I've tried all of the options except using the debugger. It doesn't get different input as far as I can see.

Computer's are deterministic. There is nothing random in a computer - not even the random number generator. If you run the following code:
  const char* line = "1 QA";
  char* end;
  for(;;)
  {
    printf("'%s' => ", line);
    double d = strtod(line,&end);
    printf("%g, %s\n", d, end);
  }

It will print out '1 QA' => 1, QA forever and ever, amen. The output will only change if the input changes. Period. So if your algorithm has not change, the problem can only be that the input has changed.

You said that the input file is the same, so, assuming the parsing code you've shown is identical, the problem must be somewhere in getting the line from the file to the parser, right? Or has something else changed?

The important thing to remember is that the bug is in your code. Even if it's not in your code, assume it is. When you find a compiler bug, keep looking until your figure out that it's actually your bug (because you will).

: I would love to use a debugger but this is on UNIX - I don't think our system has a deugger for it.

You've got GDB - a powerful command line debugger. There are several GUIs for it. My favorite is DDD.

Cheers,
Eric


Report
Re: using strod() with strange results Posted by gonzilla on 11 Jan 2002 at 11:53 AM
: You've got GDB - a powerful command line debugger. There are several GUIs for it. My favorite is DDD.

If our UNIX system has a debugger, I don't know how to access it. Can you help?

-Tyler
Report
Re: using strod() with strange results Posted by Eric Tetz on 11 Jan 2002 at 12:14 PM
: If our UNIX system has a debugger, I don't know how to access it. Can you help?

Type 'gdb' at the terminal and press ENTER.

Cheers,
Eric
Report
Re: using strod() with strange results Posted by whoie on 12 Jan 2002 at 7:48 PM
: int main (void)
: {
:     int BUFSIZE = 100;
:     int i, Index;
:     FILE *Fp;
:     char Buffer[BUFSIZ];
:     char * P, * NewLine;
: 
:     Fp = fopen("filename","r");
:     if (!Fp)
:     {
:         printf("Not able to open file!\n");
:         exit(1);
:     }
:     else
:     {
:         for (i=1; fgets(Buffer,BUFSIZ,Fp) ; i++)
:         {
:             Index = strtod(Buffer,&P);


My first question is, why are you using strtod() for an int? It would seem to me that strtol() would be a better choice. That having been said, are you positive that you have #included <stdlib.h> in your preprocessor directives? I seem to recall my own similar errors when I have used functions that return double, but an oversight on my part caused me to forget their prototype, and the compiler just assumed int. That would be some UB for you, and it could easily result in some very strange occurences.

Another option you could try, would be to test 'errno' for ERANGE, and then display 'Buffer' with perror() (all three from <errno.h>). Additionally, using an int precludes you from testing for HUGE_VAL(<math.h>), which strtod() returns on error. If you changed 'Index' to long (and then your printf() format-specifiers), you could test for LONG_MIN or LONG_MAX (from <limits.h>) on return from strtol(), and then 'errno' for ERANGE. With what you are doing right now, you might as well just use atof(), which along with its cousin atoi(), are not very good at error recognition.

:             while (isspace(*P))
:                 P++;


Oops! For portability's sake (and eradicating more UB possibilities):
           while ( isspace((unsigned char)*P) )
             ++P;




: The strange part is that it works fine as a stand alone program, but when used as part of an algorithm set for a system, strod() returns a bad index...
:
: Line 1: Bad index -268450352 for label 1 QA
:
: This is using the same exact same input file in both cases. Why would it work sometimes and not others?
:
: Thanks.
:

I am inclined to agree with Mr. Tetz on this one, but only if you have actually included a prototype for strtod(). The behavior you describe, and the output you have shown, well... *snif* *snif* ... smells like UB to me.

Good Luck!
Will

Report
Re: using strod() with strange results Posted by gonzilla on 14 Jan 2002 at 8:22 AM
: I am inclined to agree with Mr. Tetz on this one, but only if you have actually included a prototype for strtod(). The behavior you describe, and the output you have shown, well... *snif* *snif* ... smells like UB to me.

Thanks for the suggestions. The program that calls this function is a system of sorts (it manages sample being fed to a dialer and I am just trying to debug this one alg) I'm not sure that <stdlib.h> is included (I didn't write this code). I will try to figure that out.

BTW - what is UB? New to me.

-Tyler
Report
Re: using strod() with strange results Posted by whoie on 14 Jan 2002 at 12:40 PM
: BTW - what is UB? New to me.
:
: -Tyler
:

Undefined Behavior ==

ISO/IEC 9899:1999 3.4.3

1 undefined behavior
behavior, upon use of a nonportable or erroneous program construct or of erroneous data, for which this International Standard imposes no requirements

2 NOTE Possible undefined behavior ranges from ignoring the situation completely with unpredictable results, to behaving during translation or program execution in a documented manner characteristic of the environment (with or without the issuance of a diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message).

3 EXAMPLE An example of undefined behavior is the behavior on integer overflow.


Report
Re: using strod() with strange results Posted by gonzilla on 15 Jan 2002 at 2:09 PM
Hi,

Just wanted to finish out this thread with the solution for the cause of the problem which was suggested by whoie...

It is because <stdlib.h> is not used in the compilation. It was fixed when that was added.

Thanks again for all of your help!

-Tyler



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.