String variables - how to control the length?

I wonder about string variables: are they dynamically controlled?
I would expect the C language to set the string variable length at first.

How does code like this work?

String stringOne = "Hi";
stringOne += " there";

From memory allocation point of view, it requires now 8 bytes, compared to 2 at compile time.
Can Arduino handle that?

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).

...R

yigalb:
I wonder about string variables: are they dynamically controlled?
I would expect the C language to set the string variable length at first.

How does code like this work?

String stringOne = "Hi";

stringOne += " there";




From memory allocation point of view, it requires now 8 bytes, compared to 2 at compile time.
Can Arduino handle that?

Why do you use Strings for such applications?
You should use a char array. You have an option to define the length, or not define the length. If you define, then the length is locked and cannot be changed. If you not define, then it will automatically read and set the length according to the characters count in it. Use char array and not Strings.

..Arnav

From memory allocation point of view, it requires now 8 bytes, compared to 2 at compile time.
Can Arduino handle that?

The Arduino can do that, but there is a chance of heap fragmentation when using Strings. The Evils of Strings article tells how and offers advice on how to use c_strings and c_string alternative functions in their place.

yigalb:
I wonder about string variables: are they dynamically controlled?
I would expect the C language to set the string variable length at first.

How does code like this work?

String stringOne = "Hi";

stringOne += " there";




From memory allocation point of view, it requires now 8 bytes, compared to 2 at compile time.
Can Arduino handle that?

First, String is class, so has nothing whatsoever to do with “the C language”.

Next, the String “Hi there” requires a fixed six bytes of private variables (on an AVR) plus the eight bytes of “Hi there” plus a terminator, so fifteen bytes.

TheMemberFormerlyKnownAsAWOL:
First, String is class, so has nothing whatsoever to do with "the C language".

Next, the String "Hi there" requires a fixed six bytes of private variables (on an AVR) plus the eight bytes of "Hi there" plus a terminator, so fifteen bytes.

Correct about the number of bytes, yet I was wondering what happens between the first assignment and the second one to same variable. In a large scale OS, memory management systems can handle it. I wonder how will it work with a small controller like Arduino. It must be handles somehow, so it will probably take run tile resources, reducing performance.

The article that I linked explains what happens. In Arduino there is no memory management.

It must be handles somehow

Think again.

Does this help?

The way it works on Arduino is exactly how it works in other implementations . . . except, it doesn’t work.
The String class allocates memory for the new String, copies the two source Strings to the new buffer, and releases the old ones, which is where the problem comes from - soon you end up with Swiss cheese heap.

You have the source of String; why not take a look?

recently I found another string-library

Safe, Robust, Debuggable String class for Arduino

using SafeStrings is different to standard strings.

second alternativ is PStrings
both SafeStrings and PStrings can be installed with the library manager inside the Arduino-IDE.
Though you have to learn a bit through the examples and some try & error how to use them

best regards Stefan