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;
}
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;
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.
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.
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?
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
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.
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.
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.