I’m programming the ESP8266 and the ingenious Arduino libraries (Thx to Ivan and many others) seem to use strings intensively.
Does the recommendation to NOT use strings in Arduino changes when programming the ESP8266?
Using these libraries I often need to convert char arrays to strings and vice versa.
Didn’t find an analogy for the convenient string.toCharArray so I wrote my own two versions.
void charToStringL(const char S[], String &D)
{
byte at = 0;
const char *p = S;
D = "";
while (*p++) {
D.concat(S[at++]);
}
}
Both tested. The second one is twice as fast as the first one but I feel less confident with the second one as I don’t know what goes behind the scene of the assignment of a variable that goes out of scope though following tests I know its not a pointer assignment.
AbleA:
2. Using these libraries I often need to convert char arrays to strings and vice versa.
Didn’t find an analogy for the convenient string.toCharArray so I wrote my own two versions.
No need. The String class already overloads the assignment operator:
I was also quite surprised to find so much use of String in the ESP8266 core. Not only in examples, but in the actual library code too. ESP8266 does have a lot more memory but still I would expect the people involved in that project to write efficient code. Of course until just the last couple of releases there were an absolutely ridiculous number of warnings in that code and they were not fixed by a member of the core development group so maybe I'm giving them too much credit.
Strings
A typical Arduino has very limited memory and due to how Strings (with a capital S) eat and corrupt memory using Strings is, at best, frowned upon and at worst seen as pure evil. Not so with the ESP8266. The ESP8266 has a lot more memory and the fact that web pages are a lot of text, Strings are the way to go. If you do not need to edit the html at run time, char arrays are still better though.
I'm not sure why memory corruption would't be a problem with a larger memory, but so many of the library examples and tutorials for the ESP8266 use Strings that I've adopted the practice, although I never use them in my Arduino programming.
cattledog:
I'm not sure why memory corruption would't be a problem with a larger memory
It is still a problem. I just saw the first report of instability after 2 hours.
In spite of the well-documented problems (and embedded industry position), Martin Currey has decided that it is ok to use String. It's an irresponsible choice, IMO, and probably won't be fixed until enough people start complaining about instability. Even then, it's hard to point the finger at String. Sigh.