1

I've read that the Arduino String is bad as it causes memory fragmentation, so I try to avoid them mostly. But I still have some occurences where I'm not quite sure if they are ok after all.

The first is in print statements:
I have a function which prints a success or error message to the serial port, like so: Serial.println("Success");. Is this ok or should I (for example) pre define the String using #define SUCCESS "SUCCESS"?

Which leads me to the next case, defines:
I'm using MQTT and have the topics defined like above, e.g. #define TOPIC_COLOR "light/color". Is this fine?

A third one is with ArduinoJSON:
I'm using Strings as keys, like so lights["red"] = redVal;. This call is made at multiple places so I assume the string would be created multiple times? A workaround/improvement I've thought of is using an enum defining the properties and using that as key.

Thanks for all comments, tips and explanations!

5
  • 1
    You're confusing "strings" with the Arduino String class. – Majenko May 24 at 19:29
  • @Majenko So my usage of those strings is fine? – matthesinator May 24 at 19:31
  • Strings, as in "this", are just chunks of static data. It's when you start messing with String objects that problems occur. I don't know what ArduinoJSON does with string indices, though. On smaller MCUs (AVR) you should use F("stringliteral") to reduce RAM usage whenever possible, though. – Majenko May 24 at 19:35
  • @Majenko alright thank you. I don't know either but it seems commonly used so I assume it handles them good enough. Thanks for the tip! I'm on an ESP8266 right now so that won't be a problem for now – matthesinator May 24 at 19:38
  • a couple points: using Strings is fine sometimes; in setup(), static globals, consts, etc. It's more that modifying Strings is the real silent killer. The ESP also has much more ram and I've seen them "stop sinking" on code that will inevitably sink an AVR. I'm not saying String is not without pitfalls, just that not every single use is a showstopper. – dandavis May 25 at 7:06
2

+1 to @majenko's comments. I tend to avoid the String class, as I like to know what is actually going on, and I stick to char * and char [] in general (and the stdlib functions like sprintf, strlen, etc).

Couple of notes on your question:

  1. "foo/bar" is fine. Some characters have special meanings in the C language itself, so you need to escape them with the \. For example if " was part of your literal string, you'd write "foo\"bar\""

  2. I tend to use ArduinoJSON to parse. If my app is just sending JSON payloads, I just build it with sprintf or print which results in smaller programs. But, I guess it is a matter of choice. It is a great library.

1
  • Thanks! I'm using ArduinoJSON to parse and send, but also to store the current state, it's quite handy that it does all those things. As of now, storage is enough, if it gets sparse I'll have to think of something else... – matthesinator May 24 at 22:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.