What is the difference between std::string and String

Hello Arduino friends
simple Question

What is the difference between std::string and String

std:string doesn’t turn your heap into Swiss cheese.

TheMemberFormerlyKnownAsAWOL:
std:string doesn't turn your heap into Swiss cheese.

It's well explained here and more here about SRAM.

std:string treats strings (cstrings) as char arrays terminated with a NULL ('\0'). In the small memory of an Arduino the size of these arrays should be defined at compile time so the exact amount of memory, and the location in memory is pre-defined.
[Apology] It seems I got things mixed up and the std::string is actually the String class. However I was correct to say that the cstring is safer to use on Arduinos with small amounts of SRAM and is a char array terminated with a NULL[/Apology]

The String class (capital S) on the other hand allocates memory dynamically as it is required. Almost every transaction will involve the use of a different memory location. For example if you create the word "hello" and then want to extend it to "hello world" the first location is too small so a new location has to be found. Meanwhile the space that originally held "hello" is probably no use for anything else. Gradually all those unused spaces use up all the available SRAM on an Arduino.

It is, of course, possible to use cstrings with "manually" allocated dynamic memory but that would have the same effect as using the String class and should be avoided.

...R

TheMemberFormerlyKnownAsAWOL:
std:string doesn't turn your heap into Swiss cheese.

Robin2:
std:string treats strings (cstrings) as char arrays terminated with a NULL ('\0'). In the small memory of an Arduino the size of these arrays should be defined at compile time so the exact amount of memory, and the location in memory is pre-defined.

Both of these statements are incorrect.
String and std::string both dynamically allocate memory, and will both cause heap fragmentation.

They are just different implementations of the same principle. std::string is the most commonly used version, as it is part of the official C++ standard. However, on AVR Arduinos, you cannot use the C++ Standard Template Library (STL), so you cannot use std::string. That's why Arduino created its own String class, and this is the most widely used version in the Arduino ecosystem.

While both string classes use dynamic memory, most implementations of std::string have Small String Optimizations (SSO), where small strings are allocated on the stack instead of the heap. This can slightly reduce the amount of fragmentation caused by the strings.

The best string implementation to use on microcontrollers with limited RAM is null-terminated character arrays, allocated on the stack. The stack data structure ensures that memory is deallocated in the reverse order of the allocations, which makes fragmentation impossible. These strings are often referred to as "c-strings".

Pieter

std::string is not null terminated char array (a.k.a cstring). std::string is the counter part of Arduino String on different platforms.

String is supposed to be a “reduced” version of the STL string routines (std::string) that will work better on the low-memory avrs.
So I think that while String has problems and doesn’t work very well, std::string doesn’t work at all.
(Std:string might work on arms ?)

String seems to be de rigueur on the ESP platforms, and so far, for me, seems stable, but I believe heap management is more proactive on these devices (but not at zero cost)

arduino_new:
std::string is not null terminated char array (a.k.a cstring). std::string is the counter part of Arduino String on different platforms.

Thanks for the correction. I have amended my earlier Post

...R

I think Arduino (or maybe Wiring) created the String class because the original idea was to provide the equivalent of Processing for microcontrollers. Arduino's String closely matches the API of Processing/Java's String.

pert:
I think Arduino (or maybe Wiring) created the String class

Does that mean that what I have always referred to in this Forum as "String" is not in fact the standard C String class?

If so, WTF didn't they give it an Arduino-specific name to avoid confusion?

...R

Robin2:
Does that mean that what I have always referred to in this Forum as "String" is not in fact the standard C String class?

As far as the API goes, no.

Compare the APIs:

Arduino's String and Java's String are very close. Arduino's String and C++'s std::string have almost no API in common.

Robin2:
WTF didn't they give it an Arduino-specific name to avoid confusion?

My theory is it was because they wanted to provide a familiar API to Processing users. Same with the byte type. If they had given the String class a different name, it wouldn't have been so effective at that goal. Looking at it now, it doesn't seem like it is really so important of a goal, but perhaps when Arduino was getting started that Processing user base helped them get a foothold to get started on dominating the microcontroller hobby/education market.

My theory is it was because they wanted to provide a familiar API to Processing users. Same with the byte type.

In that case, the “byte” type was a spectacular fail - the Java/Processing “byte” is signed.

pert:
As far as the API goes, no.

That's a relief

...R

Maybe I was not clear, I meant "no, Arduino's String is not in fact C++ std::string".

pert:
Maybe I was not clear, I meant "no, Arduino's String is not in fact C++ std::string".

Oh sh*t ...

So now there are 3 separate things

  • cstrings that work nicely on the Arduinos with limited memory
  • The Arduino's own String class which can cause memory corruption
  • The standard C string class which can also cause memory corruption
  • The standard C++ string class which can also cause memory corruption

...R

Edited in response to Reply #16

Robin2:
The standard C string class which can also cause memory corruption

C doesn't have a string class, there's just the standard C++ std::string class.

PieterP:
C doesn't have a string class, there's just the standard C++ std::string class.

Thank you. I have corrected my Reply #15.

To be honest this is evidence of more complexity for beginners to trip over. Personally I generally don't distinguish between C and C++ because it seems rare nowadays to use C without the C++ extensions. I was not intending to distinguish between them when I wrote Reply #15

...R

Robin2:
Personally I generally don’t distinguish between C and C++ because it seems rare nowadays to use C without the C++ extensions. I was not intending to distinguish between them when I wrote Reply #15

C++ is not an extension to C. C++ is a completely separate language.

It is true that the first drafts for C++ were “C with classes” and improvements over C, which is why most (not all!) C code is valid C++ code as well, but over the past four decades, the languages have diverged.

All Arduino sketches and 99% of the libraries are compiled as C++. Talking about C in the context of Arduino is therefore irrelevant in my opinion, and it just confuses people.

I know this sounds pedantic :), but C is still very widely used (in both embedded and non-embedded environments), so it’s important to distinguish between the two languages.

The source code for arduino Strings is called “WString.*”
If you had had to #include it yourself, like in real C++, you’d have to notice...