strtod() call causes arduino to reset

Hello,

I’m having a problem converting a string to a double (or float), whenever I call strtod() the arduino resets I’ve also tried calling atof() with the same result. I’ve posted my function below, if i hardcode it to return a float everything works fine. I’m using ubuntu hardy heron if that matters. Has anyone had this problem? Or how can I troubleshoot this? Thanks

double search_string(char key, char instruction[], int string_size)
{
  char temp[10] = "";
  
  for (int i=0; i<string_size; i++)
    {
      if (instruction[i] == key)
      {
        i++;      
        int k = 0;
        while (instruction[i] != (' '|NULL))
        {
          temp[k] = instruction[i];
          i++; k++;
        }
        return strtod(temp, NULL);
      }
    }  
  return 0;
}

how can I troubleshoot this?

Serial.print(temp); just before the strtod(temp, NULL);

if temp is not what you expect then you can add some more printing to see how your parsing code is working

Thanks for the reply

Serial.print(temp); prints the string I expect.

Also I tried

return strtod(“1.0”, NULL);

which also reset the board.

The strtod function works on my arduino using verion 010

void loop(){
double d;

   Serial.print("hello ");
   d = strtod("1.0", NULL);  
   int i = (int)d ;
   Serial.print(i);
   Serial.println(" world");
}

does print: hello 1 world

Simplify your sketch to see if you are running out of memory or have some other problem elsewhere

Hello,

I’m having a problem converting a string to a double (or float), whenever I call strtod() the arduino resets I’ve also tried calling atof() with the same result. I’ve posted my function below, if i hardcode it to return a float everything works fine. I’m using ubuntu hardy heron if that matters. Has anyone had this problem? Or how can I troubleshoot this? Thanks

double search_string(char key, char instruction[], int string_size)

{
 char temp[10] = “”;
 
 for (int i=0; i<string_size; i++)
   {
     if (instruction[i] == key)
     {
       i++;      
       int k = 0;
       while (instruction[i] != (’ '|NULL))
       {
         temp[k] = instruction[i];
         i++; k++;
       }
       return strtod(temp, NULL);
     }
   }  
 return 0;
}

temp is not being properly terminated with 0 after the while loop. You need to add temp[k] = 0;

shouldn't

return strtod("1.0", NULL);

work then? It's resetting my board

shouldn’t

return strtod(“1.0”, NULL);

work then? It’s resetting my board

return strtod(“1.0”, NULL); works just fine. Maybe you have a faulty atmega168?

Post the complete code so I can try… all the examples posted so far work perfect… no reset.

Depending on the rest of your program, you may be simply using up the last bit of RAM with the strod() call. When you use too much RAM, it can wrap around to address 0 again and this has been known to cause resets.

-j

Yes, that is why adding the trailing '\0' is always a safe programming rule in C

void setup() { Serial.begin(9600); Serial.println("start"); }

void loop() { strtod("1.0", NULL);

}


That code causes my board to reset. Is it possible the string.c libraries in Ubuntu Hardy Heron are bad? I know it worked on gutsy, but then I didn't run this code for a while and I updated in the mean time. To see if it is my compiler is there a hex file generated that I could attach here? Thanks

Is it possible the string.c libraries in Ubuntu Hardy Heron are bad?

Well, yeah, possible, but not relevant to this discussion - you're using the string functions that come with Arduino, not those that come with your OS.

-j

rats, in that case is there a hex file arduino creates I could upload here for someone to try to see if it is my board or something on the compiler side?

void setup() { Serial.begin(9600); Serial.println("start"); }

void loop() { strtod("1.0", NULL);

}


That code causes my board to reset. Is it possible the string.c libraries in Ubuntu Hardy Heron are bad? I know it worked on gutsy, but then I didn't run this code for a while and I updated in the mean time. To see if it is my compiler is there a hex file generated that I could attach here? Thanks

Please, what do you mean by "reset"?

Reset as in reseting the atmel, same result as pushing the reset button on the board. If the code was

void setup() { Serial.begin(9600); Serial.println("start"); }

void loop() { strtod("1.0", NULL); Serial.println("finished"); }

finished would never be print, and start would be printed over and over (sometimes truncated or with unprintable characters added on)

I can confirm theres a problem with the compiler that ships with hardy heron, I tried my code compiled with gutsy gibbon and it works fine. I think I need to file a bug report for ubuntu, not sure for which package though.

I think I need to file a bug report for ubuntu, not sure for which package though.

Hmmm, perhaps the package that contains avr-gcc or avr libc?

Note that if your code causes "undefined" behaviour (according to the C specification) a difference of result between versions may not be considered a bug.

--Phil.

crap!

i'm having the same problem mellery. its a bummer, because the code in question is essential to the RepRap GCode interpreter. It works great on Mac OSX, but I recently switched to Ubuntu 8.04 and now the code dies.

i googled arduino strtod and i can also confirm that i'm experiencing this exact behavior. i'll see if i can find a fix / workaround.

thats my code, the problem was shipped with hardy, just grab the package from intrepid and its fixed

This may be related to the command line arguments that the Arduino IDE passes to avr-gcc and avr-g++ when compiling sketches. In particular, I think we specify that the floating printf library should not be linked, which might make a difference. You can set build.verbose to true in your Arduino preference file to see the command line used, and you might trying changing it to see if it fixes things.