When to use String

Goodevening everyone,

I'm slightly if not extremely confused about all the topics that I read on the interweb about the String class in Arduino. I've seen people get flamed for posting code when they needed help and the used String. I even read a post on here where a user replied on a question with "never, ever, ever use String". Is this true ? .. Should I never, ever, ever use String ?

Then I would like to know how the following can be achieved without using string :

void setup()
{
    tft.write("Hello World!");
}

Any help or guidance would be highly appreciate it.

Hactic:
Then I would like to know how the following can be achieved

You just did.
The **S**tring class is not involved in any way, just plain old C strings = char arrays/pointers.

Thank you kindly for your reply. So if I could choose between the following two approaches :

void setup()
{
    String myString = "Hello World!";
    tft.write(myString);
}

and

void setup()
{
    char* myString[] = {"Hello World!"};
    tft.write(myString[0]);
}

I should choose the latter ?

If you don't want to piss away memory, yes. But I would just format it as

char myString[] = "Hello world!";
tft.write(myString);

Hactic:
I even read a post on here where a user replied on a question with "never, ever, ever use String". Is this true ? .. Should I never, ever, ever use String ?

As oqibidipo mentioned, the quoted text is a plain 'ol string (otherwise known as a c-string). If you want to use the String class go for it, a lot of what you read about the String class these days is simply regurgutated by people who probably haven't used the String class themselves, but simply read about an issue that could occur in a very old version of the library.

Now it is no different to anything else, it uses memory, and you can run out of memory. This is no different to using a c-string array that is too large for the available free RAM. Using a c-string doesn't mean you can be careless with memory.

The String library does use dynamic memory and every operation can potentially allocate new memory to store new data, while freeing up old memory that is no longer used. The biggest issue is that the freed memory may be too small to use again, until another block of memory next to it is also freed up. Read up on memory fragmentation for more info.

If you stick to using the String class within functions (not global objects) all the fragments you may create are cleared when the function exits. Using it this way will allow you to get the benefits of a String with little chance of running out of memory. You can combine both options for the best usage. If you need constant data, you can use global c-strings. Then in a function you can assign it to a String and manipulate it to your needs.

pYro_65:
If you stick to using the String class within functions (not global objects) all the fragments you may create are cleared when the function exits. Using it this way will allow you to get the benefits of a String with little chance of running out of memory. You can combine both options for the best usage. If you need constant data, you can use global c-strings. Then in a function you can assign it to a String and manipulate it to your needs.

You sure about this? The dynamic memory functions will allocate from the heap, not the stack, so memory fragmentation could still occur.

KeithRB:
You sure about this? The dynamic memory functions will allocate from the heap, not the stack, so memory fragmentation could still occur.

Yes, fragments may occur while using the String class, however once the function ends the latest allocation is freed. Unless some other unrelated allocation uses one of the fragments, the memory will be returned to a contigious state (or the state it was in before entering the function). Of course there are many issues that come into play if there are other unrelated allocations created while using the String, and live longer than its allocations. But this is why I recommend keeping the String usage to functions which clearly define its lifetime.

The free algorithm is run-length just like allocation (searches heap fragments to find a space big enough for allocation). The free function will move back up the heap until it reaches the end of an active allocation, or returns to the start of the heap.

But using allocations throughout the program with potentially unknown lifetimes is not an issue for the String class, it is dynamic memory in general.

Many people programming in C++, Java, Python, etc are totally oblivious to the huge amounts of memory their programs require. What's 10 million bytes when you have 16 billion? When they bring that attitude to an Arduino UNO with 2 Thousand bytes, they are bound to crash.

outsider:
Many people programming in C++, Java, Python, etc are totally oblivious to the huge amounts of memory their programs require. What's 10 million bytes when you have 16 billion? When they bring that attitude to an Arduino UNO with 2 Thousand bytes, they are bound to crash.

I agree, but this is not specific at all to dynamic memory, and applies to using anything within the language. The key is to not make assumptions.

pYro_65:
a lot of what you read about the String class these days is simply regurgutated by people who probably haven't used the String class themselves, but simply read about an issue that could occur in a very old version of the library.

Really? I haven't seen any old rumors regurgitated lately. There was a problem years ago with memory leaks, but I haven't seen anybody suggest that it's still a problem (it's not).

I have seen several people point out

* the fragmentation problem which causes random crashes and weird behavior (still true),

* increased program space (+1600 bytes, still true),

* increased RAM usage (+10 bytes per String variable, still true) and

* increased CPU time (sometimes copying String characters during reallocation, still true).

Here is a collection of links about these subjects.

pYro_65:
If you stick to using the String class within functions (not global objects) all the fragments you may create are cleared when the function exits.

Yes, this is the most important guideline to avoid fragmentation.

You also have to be careful about passing and returning Strings, because call depth and iterations can increase the fragmentation.

And global Strings and pointers/references to Strings are definite no-nos.

The problem is that most beginners do not know about guidelines, how to apply them, how to recognize a failure, stack pointers, or the heap. They can easily get into trouble as their sketch grows: "Why does my program break when I add this line of code?" It's very easy to make a modification that breaks the guideline, like moving a section of code. This is "fragile" code.

Also, I have yet to see someone enforce a maximum String size before calling "+=". Although the String class makes sure that allocations succeed, there's no indication to the caller. The String is just emptied or the operation is not performed.

So I think it's a good guideline for beginners:

    String myString( "Hi" ); // @Hactic, avoid for now

Experts avoid it because they understand the problems and have learned better ways to manipulate, parse and print C strings:

    char myString[] = "Hi";

[Fragmentation] is not an issue for the String class, it is dynamic memory in general.

Definitely. String is just the easiest way for a beginner to get access to a pointy object.

tee hee,
/dev

And global Strings and pointers/references to Strings are definite no-nos.

Actually, passing a reference/pointer will save an otherwise unnecessary allocation (compared to by-value). Enforcing a little bit of safety with a constant reference is far more desireable than a copied String object if the data is only used, not modified.

/dev:
Definitely. String is just the easiest way for a beginner to get access to a pointy object.
/dev

And then they need to learn the c-char concept to make their code portable.

pYro_65:
Now it is no different to anything else, it uses memory, and you can run out of memory. This is no different to using a c-string array that is too large for the available free RAM. Using a c-string doesn't mean you can be careless with memory.

The String library does use dynamic memory and every operation can potentially allocate new memory to store new data, while freeing up old memory that is no longer used. The biggest issue is that the freed memory may be too small to use again, until another block of memory next to it is also freed up. Read up on memory fragmentation for more info.

Shorter version of you:

It's no different, except for this one thing that makes it different. The thing that gets brought up every time a thread about String shows up. The thing that people say is what causes the problem. They're the same, except for the troublesome part.

I'm pretty sure every coding standard for safety-critical applications (like automotive, avionics, or medical) completely forbids the use of dynamic memory allocation. So you can see that for the uses where reliability counts for everything, it's not just not used, it is avoided. You're not coding something that's controlling power steering or a life support machine (at least I hope not!), so your controller crashing because of a failure to allocate memory probably isn't such a big deal. In which case, String away.

It's just not needed in microcontroller applications. Desktop computers are absolutely drowning in an ocean of memory, most applications can be extremely wasteful and still not cause problems because there's just so damn much memory on most systems that it almost never gets fully used. Firefox is terrible about this. If you leave it open long enough it just grows and grows and grows until it's using 2GB, then I close and reopen the same tabs and it's down to 400 MB.

In a desktop OS, there's also about a bazillion different layers of indirection and abstraction between the code most people are writing and the bare hardware. All that is designed so that even if one program starts acting screwy, the rest of the system still functions and only that process gets killed.

Microcontrollers don't have that luxury. Instead of a ocean of memory, they have a puddle. There's also no OS to provide the indirection and abstract that helps guard against total system failure. If the code is irresponsible, there's a much greater chance that some exception will bring the whole system down.

In the grand scheme of things, whatever you do with your own projects isn't going to be the end of the world. (Or will it!?!?!) But if you actually want to really get into this hobby, learn the craft, and eventually take off the Arduino training wheels, it is best to avoid creating bad habits altogether. Remember that it is 1,000 harder to stop a habit than it is to start one. There is an enormous world of microcontrollers out there, not just the other AVRs (ATtiny84 and ATtiny85 are popular "next steps" to take), but entirely different manufacturers with their own unique lines of proprietary cores, like Microchip's PIC or TI's MSP lines. There's development boards coming out all over the place, like TI's Launchpad series.

Take a look at the comments...

void setup() {
//  char msg[13] = "Hello World";   // Uses 1490 bytes of code space, 198 of SRAM
  String msg = "Hello World";       // Uses 2630         "            208   "

  Serial.begin(9600);
  Serial.println(msg);
}

void loop() {
}

pYro_65:
Actually, passing a reference/pointer will save an otherwise unnecessary allocation (compared to by-value). Enforcing a little bit of safety with a constant reference is far more desireable than a copied String object if the data is only used, not modified.

Pointers and references increase the effective visibility of a String. Without using const, operations on the caller's String could interleave with a function's local String, breaking the "stack-like usage" guideline and leaving gaps in the heap when the function returns.

const pointers and const references would conform to that guideline:

    void foo( const String * a, const String & b )
    {
       *a += '!';   // compiler error, no fragmentation in caller
       b += "more"; // compiler error, no fragmentation in caller

For plain old pointers and references, an expert would have to make a judgement call as to whether a copied string would be better than fragmentation in the caller.

I hope everyone sees how overwhelming or reeediculous this might seem to a beginner. Just use char arrays and you won't have to know all these minutiae. :stuck_out_tongue:

Cheers,
/dev

/dev:
Pointers and references increase the effective visibility of a String. Without using const, operations on the caller's String could interleave with a function's local String, breaking the "stack-like usage" guideline and leaving gaps in the heap when the function returns.

And like I have mentioned above in post #8, this has nothing to do with Strings.

If you pass String 'A' by reference to a function and modify it in said function, it is no different to doing the same operations in the calling function.

The fact that you decide to create another local String, or perform any other unrelated allocation is a choice that needs appropriate consideration with regards to the potential problems that can occur. This is not a String issue, but something that could occur with any dynamic memory usage.

Using a char array, while applying unsafe practices with dynamic memory is still going to land you in this situation.

But in the situation you have mentioned this is most probably all worry for nothing as once the String passed by reference is freed, the heap pointer will return to where it was at the start of the function. The only issue is the possibility that there may be fragments until the calling function returns. The memory is not lost forever.

Totally agree, especially that this is not unique to String, at the end of reply 9. I can't agree any more! :). The OP's question was about String, so I am trying to keep the answers related. Phew!

/dev:
Totally agree, especially that this is not unique to String, at the end of reply 9. I can't agree any more! :). The OP's question was about String, so I am trying to keep the answers related. Phew!

Yeah, it is easy to take this off on a tangent.

Anyway, the issues you pointed out with regards to references and pointers are extermely minimal, as the fragments disappear once the top tier String is freed. And even on an Uno if you have 1k free of ram, that is plenty of room to have 10's of temporary fragments without issues. As there is a defined lifetime, this is quite a reasonable practice even for such a small device.

The real issue is doing the same thing with global Strings as unless all intertwined allocations are freed, then fragments stay forever. But I beleive we all agree that this is nothing short of "willingly shooting one's self in the foot", and has staggeringly adverse consequences compared to the 'function only' usage I have mentioned.

Gasp!

KeithRB:
You sure about this? The dynamic memory functions will allocate from the heap, not the stack, so memory fragmentation could still occur.

That's true, but at function exit C++ destructors are called which should (mostly) put back the heap into the state it was before you called the function.

The destructors will not magically take care of fragmentation, which is the whole point of this.