Better way to use Sting?

I find a lot threads suggest avoid using Strings or too many Strings and understand the basic reason (leaving holes in memory during operation). But someone mentioned that one can use reserve() function to reserve some memory for Strings. Will this make using String especially multiple Strings safe?

I need to use String to receive a sentence from serial and check words in it so need the features of Strings and the flexibility of it, otherwise, do I need to use char array exclusively?

I have seen references to the SafeString library that makes using Strings safer. I have not used the library, but it may help you.

SafeString even offers non-blocking receiving strings over the serial interface.

Another possaibilty is to use the PString-library. WHich is based on char-arrays. Very safe to use.
PString offers almost the same comfort as Strings ( al little less)

Your "sentence" has a maximal possible length. The basic principle is do define a string of a maximum length.

The SafeString-library comes with a lot of examples on how to do this
But you still will need some time to work through it.
I recommend to reduce the example-codes of SafeString to just do one thing.

best regards Stefan

Ask Stewart Copeland?

TheMemberFormerlyKnownAsAWOL:
Ask Stewart Copeland?

For beeing a HAL9000 your postings are almost as short as the answers of deepthought.

I use a String buffer sized to accommodate the largest expected String size.

String sX;
 voide setup()
{
sX.reserve(300);
}

To put into a String buffer use concatenate and to clear a String buffer use "".

sX.concat( "qwerty" ); //String stores qwerty
sX.concat" " asd" ); //String adds asd to sX and now contains qwerty asd
sX = ""; // String buffer empty

The ram/stack space the String buffer is set and does not shrink or grow.

See String() - Arduino Reference

StefanL38:
For beeing a HAL9000 your postings are almost as short as the answers of deepthought.

"Brevity is the soul of wit", Hamlet, Act 2

I need to use String to receive a sentence from serial and check words in it

No, you don't need it. That can be done relatively easily with C strings (character arrays).

StefanL38:
SafeString even offers non-blocking receiving strings over the serial interface.

Another possaibilty is to use the PString-library. WHich is based on char-arrays. Very safe to use.
PString offers almost the same comfort as Strings ( al little less)

Your "sentence" has a maximal possible length. The basic principle is do define a string of a maximum length.

The SafeString-library comes with a lot of examples on how to do this
But you still will need some time to work through it.
I recommend to reduce the example-codes of SafeString to just do one thing.

best regards Stefan

The library seems to be a very good and complex one. Yes, I have only three Strings to deal with, and have put the reserve() in my code for now to limit the length. Is this reserve() safe (enough)?

I will play with the library later.

Idahowalker:
I use a String buffer sized to accommodate the largest expected String size.

String sX;

voide setup()
{
sX.reserve(300);
}




To put into a String buffer use concatenate and to clear a String buffer use "".



sX.concat( "qwerty" ); //String stores qwerty
sX.concat" " asd" ); //String adds asd to sX and now contains qwerty asd
sX = ""; // String buffer empty




The ram/stack space the String buffer is set and does not shrink or grow.

See https://www.arduino.cc/reference/en/language/variables/data-types/stringobject/

Thanks. That's exactly what I want to do first. Have you used this strategy a lot and did you get any issue because of it?

joedodo:
The library seems to be a very good and complex one. Yes, I have only three Strings to deal with, and have put the reserve() in my code for now to limit the length. Is this reserve() safe (enough)?

I will play with the library later.

I had a Arduino Uno running, continuous, for 2.5 years using Strings and reserve.

What do you mean by safe?

TheMemberFormerlyKnownAsAWOL:
Ask Stewart Copeland?

Alexa, tell me your service tag No. please

Maybe ask Gordon Sumner instead. Drummers never know.

CrossRoads:
Maybe ask Gordon Sumner instead. Drummers never know.

I didn't quiet get her/his/its (HAL9000s) point, does she/he/it imply those Strings behavior so unpredictablly that nobody here can tell exactly what can happen even I use reserve() function? Or she/he/it just said I have asked a nonsense question?

joedodo:
I didn't quiet get her/his/its (HAL9000s) point, does she/he/it imply those Strings behavior so unpredictablly that nobody here can tell exactly what can happen even I use reserve() function? Or she/he/it just said I have asked a nonsense question?

what will happen if variables get overwritten highly depends on what variable and how this variable is used in the code. So yes it is unpredictable.
Anyway writing code that has a danger of writing into RAM at the wrong places is bad.
This is one of the BIG BIG flaws of C++. Almost any other porgramming-language has better protection mechanisms to avoid writing into the wrong places. So for a beginner-friendly programming language - which was the original intend of the Arduino-IDE-System the decision to choose C++ was bad. The big advantage is that C++ is an industrial standard which means there are a lot of libraries "ready to use"

The avatar of user TheMemberFormerlyKnownAsAWOL is the "Eye" of the ship-control-computer called HAL9000 from the science-fiction-film "2001 odyssee in space" Where the TLA "HAL" was chosen as a Three Letter Acronym with letters each one letter befor the TLA "IBM" "H" one befor "I" "A" one before "B" "L" on before "M"
HAL9000 is a real artificial intellgigence that sees itselfs as infallible. But makes a mistake and starts killing the crew.
deepthought is a ultra-hyper-super-computer from the scifi-film "hitchhikers guide through the galaxy" who was asked "“what is the meaning of life, the universe and everything”" He was "thinking" about it for 7,5 million years. And his answer was very short. You can ask google “what is the meaning of life, the universe and everything” to retrieve the answer. ;-)))

So some kind of "insider"-joke
what Sting (Gordon Summer) Lead-singer of the band ploice and Stewart Copeland Drummer of the Band police have to do with it - no idea. maybe some other "insider-joke I don't know
best regards Stefan

StefanL38:
So some kind of "insider"-joke
what Sting (Gordon Summer) Lead-singer of the band ploice and Stewart Copeland Drummer of the Band police have to do with it - no idea.

Wow! And I thought us softies were all supposed to have superhuman attention-to-detail.

Just to clarify:-

The topic title is "Better way to use Sting?"

It was begging for a comment, such as Awols :slight_smile:

Thanks Stefan, especially the background part. Peronally, I like the Police, with their reggae and some other elements.

For Strings, I thought once I reserve() the ram for them (as long as I don't make mistake making them overlapping or something), they should be well defined and have fairly predictable behavior (since they have boundaries already). No?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.