using strod() with strange results

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!
");
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,'
');
if (NewLine)
{
*NewLine = '';
}
printf("Label is %s index is %d
", 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.

Comments

  • : 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:

    [b](a)[/b] 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:
    [code=ffffff]
    printf([color=bb0000]"'[/color][color=907050]%s[/color][color=bb0000]'[/color][color=907050]
    [/color][color=bb0000]"[/color], Buffer);
    [/code]
    To show what it is you're parsing.

    [b](b)[/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 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:
    :
    : [b](a)[/b] 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:
    : [code=ffffff]
    : printf([color=bb0000]"'[/color][color=907050]%s[/color][color=bb0000]'[/color][color=907050]
    [/color][color=bb0000]"[/color], Buffer);
    : [/code]
    : To show what it is you're parsing.
    :
    : [b](b)[/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.
  • : 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:
    [code=ffffff]
    [color=000080][b]const[/b][/color] [color=000080]char[/color]* line = [color=bb0000]"1 QA"[/color];
    [color=000080]char[/color]* end;
    [color=000000][b]for[/b][/color](;;)
    {
    printf([color=bb0000]"'[/color][color=907050]%s[/color][color=bb0000]' => "[/color], line);
    [color=000080]double[/color] d = strtod(line,&end);
    printf([color=bb0000]"[/color][color=907050]%g[/color][color=bb0000], [/color][color=907050]%s[/color][color=907050]
    [/color][color=bb0000]"[/color], d, end);
    }
    [/code]
    It will print out [blue]'1 QA' => 1, QA[/blue] 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 [italic]file[/italic] 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


  • : 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
  • : 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
  • [code=ffffff]: 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!
    ");
    : exit(1);
    : }
    : else
    : {
    : for (i=1; fgets(Buffer,BUFSIZ,Fp) ; i++)
    : {
    : Index = strtod(Buffer,&P);[/code]

    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 [red][b][italic]positive[/italic][/b][/red] that you have #included in your preprocessor directives? I seem to recall my own similar errors when I have used functions that return [b]double[/b], but an oversight on my part caused me to forget their prototype, and the compiler just assumed [b]int[/b]. 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 ). Additionally, using an [b]int[/b] precludes you from testing for HUGE_VAL(), which strtod() returns on error. If you changed 'Index' to [b]long[/b] (and then your printf() format-specifiers), you could test for LONG_MIN or LONG_MAX (from ) 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.

    [code=ffffff]: while (isspace(*P))
    : P++;[/code]

    Oops! For portability's sake (and eradicating more UB possibilities):
    [code]
    while ( isspace((unsigned char)*P) )
    ++P;
    [/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?
    :
    : 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

  • : 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 is included (I didn't write this code). I will try to figure that out.

    BTW - what is UB? New to me.

    -Tyler
  • : BTW - what is UB? New to me.
    :
    : -Tyler
    :

    [b]U[/b]ndefined [b]B[/b]ehavior ==

    ISO/IEC 9899:1999 [b]3.4.3[/b]

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

    [size=1]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.[/size]

  • 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 is not used in the compilation. It was fixed when that was added.

    Thanks again for all of your help!

    -Tyler
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories