buffer overflow with string to float

Hey everyone

I am making my own activity watch with a user application for on the desktop. Now i want i got a bit of code that overflows (i think) my arduino. it runs the function 1 or 2 times and then it just stops working, it even removes itself from my com ports.

In the code below i try to convert a string to a float. can someone help me ?

 float startLong;
 char char1[8];
 sGPSlongitude.toCharArray(char1,sGPSlongitude.length()+1);
 startLong = atof(char1);
 
 
 float startLat;
 char char2[8];
 sGPSLatitude.toCharArray(char2,sGPSLatitude.length()+1);
 startLat = atof(char2);
 Serial.println("startlat :");
 Serial.println(startLat,7);

One possibility is that char1 and char2 aren't big enough. Serial.print sGPSlongitude.length()+1 to find out.

and do not forget the terminating \0

What exacly do you mean with terminating ?

Adding a '\0' at after all the characters

Hey

I did a serial print for the length and it is 9. So i changed that but it still crashes.

Do i concat he string with /0 or something else ?

Do i concat he string with /0 or something else ?

No, not '/0', but '\0'.

hey

I tried the following but it didn't work:

  String sGPSlongitude;
  sGPSlongitude.concat(EEPROM.read(2));
  sGPSlongitude.concat(".");
  sGPSlongitude.concat(EEPROM.read(3));
  sGPSlongitude.concat(EEPROM.read(4));
  sGPSlongitude.concat(EEPROM.read(5));
  sGPSlongitude.concat("\0");
  
  String sGPSLatitude;
  sGPSLatitude.concat(EEPROM.read(6));
  sGPSLatitude.concat(".");
  sGPSLatitude.concat(EEPROM.read(7));
  sGPSLatitude.concat(EEPROM.read(8));
  sGPSLatitude.concat(EEPROM.read(9));
  sGPSLatitude.concat("\0");
   
  String sDistance;
  sDistance.concat(EEPROM.read(0));
  if(decimalDistance == true)
  {
    sDistance.concat(EEPROM.read(1));
  }
  
 float startLong;
 char char1[11];
 Serial.println(sGPSlongitude.length()+1);
// sGPSlongitude.toCharArray(char1,sGPSlongitude.length()+1);
// startLong = atof(char1);
 
 
 float startLat;
 char char2[11];
 Serial.println(sGPSlongitude.length()+1);
 sGPSLatitude.toCharArray(char2,sGPSLatitude.length()+1);
 startLat = atof(char2);
 Serial.println("startlat :");
 Serial.println(startLat,7);
sGPSlongitude.concat("\0");

{ sigh }

There's a HUGE difference between "\0" and '\0'.

Please, just ditch the String class.

 sGPSlongitude.toCharArray(char1,sGPSlongitude.length()+1);

This is total crap. The second argument is the size of the array you are trying to write to, NOT the number of characters in the String being operated on.

  String sGPSlongitude;
  sGPSlongitude.concat(EEPROM.read(2));
  sGPSlongitude.concat(".");
  sGPSlongitude.concat(EEPROM.read(3));
  sGPSlongitude.concat(EEPROM.read(4));
  sGPSlongitude.concat(EEPROM.read(5));
  sGPSlongitude.concat("\0");

More crap. EEPROM.read() does NOT return a char. That it returns something the same SIZE is completely irrelevant.

Ditch the stupid String class all together. You KNOW how many characters need to be in the array. Just use a char array of the right size from the beginning.

The char array has to be at least one element bigger than the actual data you want to write into it.

If your number is going to be 9 characters long, you need char value[10].

And after your 9 characters, you need to put a char '/0', which is a byte with the number 0 in it 00000000 binary 0x00 hexadecimal, the 8-bit unsigned number 0.

And remember to put this into value[9] not into value[10] because the indexes of your ten element array run from 0 to 9 not to 10.

you need to put a char '/0',

Should be '\0'

All the same result:
char term = '\0';
char termin = NULL;
char terminator = 0;

char buffer[ 12 ] = "ABCDEFGHIJ";
Serial.println( buffer ); // prints ABCDEFGHIJ
buffer[ 4 ] = 0;
Serial.println( buffer ); // prints ABCD