Go Down

Topic: Is String still bad? (Read 3009 times) previous topic - next topic

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?

Delta_G

#1
Dec 09, 2015, 04:23 pm Last Edit: Dec 09, 2015, 04:24 pm by 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.
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

RayLivingston

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.

KeithRB

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.

aarg

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?
  ... with a transistor and a large sum of money to spend ...
Please don't PM me with technical questions. Post them in the forum.

AWOL

There may well be, but like tidying your workshop, there's never a good time.

aswine

Is there less reason to avoid String on a Mega2560 than on something with much less memory?

AWOL

No, not really.
Try not to get into the habit of using it, and you'll stay saner, longer.

KeithRB

Is there any reasonably easy way to defrag the heap?
Push the reset button. 8^)

econjack

Consider the following simple program:

Code: [Select]

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:

http://www.tutorialspoint.com/c_standard_library/string_h.htm


aarg

Push the reset button. 8^)
I can't. It's a remote system. :)
  ... with a transistor and a large sum of money to spend ...
Please don't PM me with technical questions. Post them in the forum.

jurs

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.


zoomkat

Quote
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.

https://forum.arduino.cc/index.php?topic=353834.0
Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

AWOL

Quote
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.

PaulS

Quote
Careful code development will probably eliminate any String dynamic memory issues.
Very true, because careful code development means not using Strings.
The art of getting good answers lies in asking good questions.

Go Up