1

This seems like an old, solved problem, but I can't find any simple answers. I'm reading latitude and longitude data as Strings from a file (about 50 points), but they are really double float numbers.

First, I want to find the min/max ranges. Then, later, I want to do some calculations on them. I don't really want to keep reading the file, so I want to put them in an array. But no matter what kind of array I use (3 examples below), the program crashes with stack errors.

const char *latArray[ptsN];
const char *lngArray[ptsN];
// or
String latArray[ptsN];
String lngArray[ptsN];
// or
double latArray[ptsN];
double lngArray[ptsN];
while( myFile.available() ) {
 lineN++;
 String latGet = myFile.readStringUntil(',');
 double dLat = StrngToDouble( latGet );
 if( dLat < minLat ) minLat = dLat;
 if( dLat > maxLat ) maxLat = dLat;
 latArray[lineN] = latGet; // put in String array
 // or
 latArray[lineN] = latGet.c_str(); // put in const char * array
 // or
 latArray[lineN] = dLat; // put in double array
 String lngGet = myFile.readStringUntil(','); 
 double dLng = StrngToDouble( lngGet );
 if( dLng < minLng ) minLng = dLng;
 if( dLng > maxLng ) maxLng = dLng;
 lngArray[lineN] = lngGet; // put in String array
 // or
 lngArray[lineN] = lngGet.c_str(); // put in const char * array
 // or
 lngArray[lineN] = dLng; // put in double array
 String tmp = myFile.readStringUntil('\n');
}

where

double StrngToDouble( String dblString ) {
 double temp = strtod( dblString.c_str(), NULL );
 return temp;
}

This can compile and work - EXCEPT for reading the arrays and calculating using the floats, no matter what type of arrays I try using. It just crashes. What's the best way to resolve this? Thanks, Rick

asked Jan 6, 2019 at 19:58
9
  • You may be out of memory. What Arduino are you using? How large is lineN? How much RAM is statically allocated (the compiler tells you that)? Commented Jan 6, 2019 at 20:02
  • I'm actually using a Wemos ESP32. The compiler says: Sketch uses 285032 bytes (27%) of program storage space. Maximum is 1044464 bytes. Global variables use 37092 bytes (45%) of dynamic memory, leaving 44828 bytes for local variables. Maximum is 81920 bytes. Commented Jan 6, 2019 at 20:07
  • And lineN?... Commented Jan 6, 2019 at 20:09
  • Simplify your program. Read the data and print out what you get. Don't do anything with arrays yet. Once you have it reading and converting fine you can look at creating the arrays and adding the data to them. To me it sounds like you may be having a buffer overrun. Too many entries being put into your arrays. Commented Jan 6, 2019 at 20:25
  • And to back that up - the first thing you do in your loop is lineN++, so you start with slice 1 instead of slice 0. Do you then go to fill slice ptsN? That's one more slice than you have... Commented Jan 6, 2019 at 20:27

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.