I've been reading bunch about String. Much of what I've found is from several years ago. At that time, most people were strongly recommending that no one use it for serious projects. Here in December 2015 and Arduino IDE 1.6.6, I'm hoping that those problems have been fixed. Do the experts in the Arduino community still have the same opinion? Has String been updated or fixed significantly in the past few years? Are there good third-party alternatives? Should I look into and use Paul Stroffregen's String library as mentioned here?
Your question is based on am entirely false premise. Contrary to what is so often espoused here, there is nothing inherently "wrong" with String that needs "fixing". As with all things, String has its benefits, and its costs. There are valid uses for it, but it has its down-sides. The two primary down-sides are those that come with use of ANY "object" - it uses memory, and improper use can lead to problems, in particular fragmentation of the heap. Creating, and destroying, ANY object can lead to this problem, it is not unique to String. However, virtually anything that can be done with String, and also be done without String, and those alternative approaches will typically use less memory (both RAM and FLASH), and will generally not have as great a risk of heap fragmentation.
Regards,
Ray L.
I guess the point is why bother? Unless String does something that you have absolutely no idea how to program, it is very simple to do compares and concatenations with the lightweight C string.h, and you have total control of the memory footprint.
Delta_G:
The memory leak was fixed, but they still use up a lot of RAM and fragment the heap. They will always do that. It is the nature of using dynamic memory allocation the way they do. It is still best to avoid them on RAM limited systems like a microcontroller.
Is there any reasonably easy way to defrag the heap?
There may well be, but like tidying your workshop, there's never a good time.
Is there less reason to avoid String on a Mega2560 than on something with much less memory?
No, not really.
Try not to get into the habit of using it, and you'll stay saner, longer.
aarg:
Is there any reasonably easy way to defrag the heap?
Push the reset button. 8^)
Consider the following simple program:
void setup() {
//String message = "This is a message";
char message[] = "This is a message";
Serial.begin(9600);
Serial.print(message);
}
void loop() {
}
The version that uses the String class compiles to 3186 bytes and 210 byte of SRAM. The C string version compiles to 1804 bytes and 200 bytes of SRAM; a savings of over 40% of program memory. One of the reasons is because the String class brings in a lot of functionality that goes unused in this program. However, even adding strcpy() and strcat(), probably two of the most common functions performed on strings, the code size will still favor the C string. A pretty good summary can be seen at:
KeithRB:
Push the reset button. 8^)
I can't. It's a remote system.
aswine:
I've been reading bunch about String. Much of what I've found is from several years ago. At that time, most people were strongly recommending that no one use it for serious projects. Here in December 2015 and Arduino IDE 1.6.6, I'm hoping that those problems have been fixed. Do the experts in the Arduino community still have the same opinion? Has String been updated or fixed significantly in the past few years? Are there good third-party alternatives? Should I look into and use Paul Stroffregen's String library as mentioned here?
When Arduino 1.0 was released, the "String" class was faulty due to compiler/library problems and could easily create wrong results and programs "hanging".
This problem was fixed at some point, I think with release of IDE 1.0.5.
So at least the "String" class is working "error free" now, if that was your question.
But of course "String" is BAD BAD BAD, because of:
- using more RAM than actually needed to solve any problem
- encourages programmers to write programs that eat up more RAM than is actually available
- slowing down program execution which might be bad if every microsecond is counting in your application
- fragments RAM, so that after some time of running an application the RAM could be fragmented too much, so that no "String" of certain length can be allocated any longer.
The "alternative" is using "nullterminated strings" of course, which are the native string type for the C/C++ programming language.
I've been reading bunch about String.
In these post, note the difference usage of "will" and "could" in the post. More "program" memory is used using Strings to support the String functions. Careful code development will probably eliminate any String dynamic memory issues. If you are just getting started, use what you understand and go from there. Below is a recent discussion you can read.
More "program" memory is used using Strings to support the String functions.
Don't forget also that every instance of a String uses up six bytes of RAM, even if the String is empty.
In large arrays of Strings, this soon adds up to a lot of wasted RAM.
Careful code development will probably eliminate any String dynamic memory issues.
Very true, because careful code development means not using Strings.
Sigh.
doomkat:
If you are just getting started, use what you understand and go from there.
Yes, by all means, if you truly understand the String
interface (i.e., the header, which is easy), its implementation (i.e., the .CPP, of medium difficulty), its use of the heap and memory fragmentation (advanced topics), the program space/RAM/CPU time implications (stated above), and programmatic determinism (valued by experts), then please use String
.
And if you get into trouble, you obviously didn't understand something. Wait, if you understand all those things, you would never use String
.
This is the central problem of String
: its interface is easy to understand, but its implementation makes the long-term prospects of the program very poor, in a very subtle, hard-to-understand way.
zoomkopf:
Below is a recent discussion you can read.
Yes, that's a great thread. But after a lengthy, rational description of the pros (ease-of-use) and cons (everything else), the 3 proponents declare:
- 70% of all Arduino programs could use
String
- 15000-line programs can be written with
String
String
should not be avoided dogmatically
-_-