pYro_65:
a lot of what you read about the String class these days is simply regurgutated by people who probably haven't used the String class themselves, but simply read about an issue that could occur in a very old version of the library.
Really? I haven't seen any old rumors regurgitated lately. There was a problem years ago with memory leaks, but I haven't seen anybody suggest that it's still a problem (it's not).
I have seen several people point out
* the fragmentation problem which causes random crashes and weird behavior (still true),
* increased program space (+1600 bytes, still true),
* increased RAM usage (+10 bytes per String variable, still true) and
* increased CPU time (sometimes copying String characters during reallocation, still true).
Here is a collection of links about these subjects.
pYro_65:
If you stick to using the String class within functions (not global objects) all the fragments you may create are cleared when the function exits.
Yes, this is the most important guideline to avoid fragmentation.
You also have to be careful about passing and returning Strings, because call depth and iterations can increase the fragmentation.
And global Strings and pointers/references to Strings are definite no-nos.
The problem is that most beginners do not know about guidelines, how to apply them, how to recognize a failure, stack pointers, or the heap. They can easily get into trouble as their sketch grows: "Why does my program break when I add this line of code?" It's very easy to make a modification that breaks the guideline, like moving a section of code. This is "fragile" code.
Also, I have yet to see someone enforce a maximum String size before calling "+=". Although the String class makes sure that allocations succeed, there's no indication to the caller. The String is just emptied or the operation is not performed.
So I think it's a good guideline for beginners:
String myString( "Hi" ); // @Hactic, avoid for now
Experts avoid it because they understand the problems and have learned better ways to manipulate, parse and print C strings:
char myString[] = "Hi";
[Fragmentation] is not an issue for the String class, it is dynamic memory in general.
Definitely. String is just the easiest way for a beginner to get access to a pointy object.
tee hee,
/dev