Optimizing String usage - print()

Hello,

Recently I've been working on the usage of String in my ESP8266 Firmware code as I had memory reallocation and inefficient usage of the resources. I have been reading carefully the following guide from drmpf:

At some point, he says:

What is a really simple way of fixing the way Strings are built. However, I was wondering about the print function. Does it affect the fact of using the operator '+' to print a large String? Is this creating short lived temporary Strings?

Thanks,
Diego

The advice that you will get here will most likely be not to use Strings in the first place, but to use C style strings, ie zero terminated arrays of chars

2 Likes

The author is also on this forum. Let's get his attention: Hello @drmpf are you still on this forum ?

1 Like

Yes. The advice to avoid adding Strings is good.

The advice to avoid Strings altogether is better.

No matter you continue to use Strings or not, learning how to do without is essential.

a7

I actually don't want to adapt all my code to use SafeString, but there are some interesting notes in their documentation about the usage of String:

Definitely a debate.

He actually says they are not that bad if you use them correctly:

Arduino Strings are not as bad as they have been made to be and by following a few guidelines, can be used with confidence in your sketches. Arduino Strings use minimal extra SRAM memory over a plain char[], about 8bytes/String, but that 8 bytes provides you with complete safety from buffer overruns and missing string terminators as well as convenient coding methods.

If you want to keep complete control over your memory usage or you are working with a library that returns a char* or you want a richer set of readers, parsers and text functions or want detailed debugging/error messages, then you can install the SafeString library from the Arduino library manager.

Either of these alternatives, String or SafeString, are preferable to using error prone low level char[] manipulations and c-string methods, like strcat and strcpy, which are the systemic cause of so many programming errors.

I've got my :popcorn: ready

2 Likes

Yes, a debate, opinions and stuff.

Please learn how to use C/C++ character arrays. Whether or not you continue to use Strings or SafeStrings or whatever.

That's all I am saying. I never use Strings because I grew up when there was no such thing, and I started with tiny microprocessors where using anything like that would have been ridiculously ridiculed as well as not working very well.

a7

1 Like

the SafeString library is written using the "dangerous" functions the authors does not like - yet he relied on those to write his library... (let's not start that debate again !)

that's because c-strings are not bad if you use them correctly.

Knowing to deal with buffers (arrays) and ensuring you are staying within the bounds of your buffer is a critical notion to master, be it for strings or general purpose buffers. So that's something any wannabe developper should master

Once you have that skill, you can decide what you want to use

I sort of see String as a gatekeeping method to keep noobs from learning the good stuff. You don't get to do anything cool with any sort of programming until you understand arrays. And the best way to learn about using them is by playing with strings.

So someone told all the noobs that arrays were hard and handed them a String class and it stops them from learning the good stuff. And then they come and argue with us that they shouldn't have to be allowed to learn the good stuff. It's sad really.

For prints using String + the following works well

 {
 String str = String ... + .... 
 Serial.println(str);
 }

Any memeory allocated is completely recovered at the closing } without any holes in the memeory

But being ESP you still need to restart regularly to clean up the memory.
Either from the ESP's own String use or from memory leaks in its support code.

see Taming Arduino Strings Avoiding Fragementation and Out-of-Memory Issues

Also see the code in GitHub - boblemaire/IoTaWatt at 02_06_03
for how to monitor the free memeory and reboot as necessary.
(not sure exactly where in there, but I can see the logs saying low memory restart)

I didn't want to refute you, I am actually in a process of learning and the forum and the users' opinions are extremely valuable for me.

However, I don't always take someone's opinion as the absolute truth, and I have seen a lot of controversy on this issue. That's why I wanted to have the debate.

Thank you so much for the information, I will study C char arrays deeply :stuck_out_tongue:

Why? Do you think it will come to any conclusion? Have you not seen other examples of this debate? It's just a bunch of conflicting opinions. That's why all you see is controversy.

Understood, I've noticed that many of you share the same opinion. I will study them!

Thank you so much for your response.

Just as you say, I have seen a bunch of conflicting opinions.

When it comes to something as code developing, I had never seen this kind of controversy, so I wanted to have a justified answer to why are things done like this. Even to why String exists when is not even necessary.

instead of concatenating Strings before printing, just print the parts one by one.
Or even just stream to your printable hardware.

It depends on what you want to achieve.

What does your sketch look like today?

But all you'll get is a bunch of conflicting opinion. Why do you expect anything different this time?

Most of us agree that String is not the best. I think everyone knows that and there have been countless articles written on why.

Of course there are dissenters. And they'll say what they'll say. I don't see how this discussion can be expected to go any differently.

Thanks for coming!

I will take a look to the links, even though from this post's responses I think I will get to study C char arrays and try to stop using Strings.

Well I guess I understood it now.

I'm in the process of optimizing my code and the different opinions on this topic had confused me. Everthing is clear now, thank you so much :smiley:

You certainly need to understand char arrays (and arrays in general)
But avoid the low level c-string routines unless you are a perfect programmer who never makes mistakes.
Strings were introduces over 20 years ago in response to the persistant coding errors in using c-strings.
Those who do not study history are doomed to make the same mistakes.

1 Like