String vs std::string

Some years back I was warned of using the Arduino String class by a very experienced developer so I've avoided it ever since. Of course when a non-devloper like me uses char without due care all sorts of weird behavior can arise, usually due to memory overwrites caused by poor pointer control.

Now I'm reading non-arduino info on the std::string class and wondering why Arduino bothered to add the String class? And how to use the std::string class in Arduino.

As a primer, I asked Google's AI engine Bard : see below.
Did Bard get it right? :sunglasses:

Q: In Arduino what is the String type, when compared to std::string in C++?

Response:
Both Arduino's String type and C++'s std::string are used for representing text data, but they have some key differences:

Memory Management:

Arduino's String: It manages its own memory dynamically, which means that the Arduino runtime will automatically allocate and deallocate memory as needed. This makes it easier to use, but it can also lead to memory fragmentation issues if not managed carefully.

C++'s std::string: It relies on the programmer to manage memory explicitly. This gives more control over memory usage, but it also requires more careful programming to avoid memory leaks.

Performance:

Arduino's String: It is generally less performant than C++'s std::string due to its dynamic memory management. This can be noticeable in performance-critical applications.

C++'s std::string: It is typically more performant than Arduino's String due to its explicit memory management. This makes it a better choice for applications where performance is a major concern.

String Manipulation:

Arduino's String: It provides a set of methods for manipulating strings, such as length(), substring(), and concat().

C++'s std::string: It also provides a rich set of methods for string manipulation, which are generally more powerful and flexible than those of Arduino's String.

Overall:

Arduino's String: It is easier to use and does not require explicit memory management, making it a good choice for beginners and for applications where performance is not a critical concern.

C++'s std::string: It offers more control over memory usage and is typically more performant, making it a better choice for advanced programmers and for performance-critical applications.

IDK, I don't use either. But character arrays are not all that difficult, no one was born knowing anything at all and usually ppl can catch on.

I encourage you to come to grips with so-called small-s strings as it will also mean learning some other basic C/C++ stuff around arrays and pointers.

All of which you sooner later will really be better off being able to exploit.

a7

That's my aim, but when I try to use small-s string it doesn't complile, and this doesn't help.

#include <strings.h>

I'd guess because the C++ implementations for the original 8-bit AVR-based Arduino boards did not include many of the classes from the standard C++ library (i.e. the std:: scope). This was due to the limited memory available to those 8-bit processors. But, the developers of Arduino apparently took pity on newbies who couldn't live within the bounds of arrays they themselves created. So, the benevolent creators implemented a custom String class.

Nope. std::string blah = "blah"; blah = blah + "foo";. No explicit memory management there. And blah is completely freed automatically when its lifetime ends.

I didn't even bother reading the AI-generated drivel.

Here's where we say "post the code" and try to help.

So next time, try again and if nothing you usually do to learn new things about C++, come back.

a7

I believe the technical term for that is hogwash. Or is it drivel?

Please read this: The Evils of Arduino Strings | Majenko's Hardware Hacking Blog

There is a SafeString class that is supposed to make using Strings easy, but really, c-strings (null terminated character arrays) are not hard to use and are robust.

2 Likes

Thanks @groundFungus that's a good read, although it doesn't show how to use std::string which is why I opened this thread. I probably won't use std::strings but I want to get the big picture on string usage in C/C++ (not just Arduino)

Seems a reasonable "guess" to me :slightly_smiling_face:.

But is it even possible to use the small-s C++ string instuction, as described here Stings in C++ on GeeksforGeeks, in Arduino?

Off topic: well I say you should bother :slightly_smiling_face: Bard AI is not always drivel, Although it's not trustworthy on overviews like my example, it is quite useful for generating code when asked a good question. I've not tried chatGPT yet, but I understand Bard is becoming quote popular amongst coders.

As far as overviews go, I treat Bard just like a knowledgeable human. It "knows" heaps but can't always be trusted. By using it on topics you know one gets a sense of trustworthiness over time , just like a knowledgeable human.

Here's an example:

Me: provide an example function in C++ that receives a character string as input, and outputs a modified character string

BARD: Sure, here is an example function in C++ that receives a character string as input and outputs a modified character string:

#include <iostream>

using namespace std;

string modifyString(string inputString) {
  string outputString = "";
  for (char c : inputString) {
    if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
      c = toupper(c);
    }
    outputString += c;
  }
  return outputString;
}

int main() {
  string inputString;
  cout << "Enter a string: ";
  cin >> inputString;
  string outputString = modifyString(inputString);
  cout << "Modified string: " << outputString << endl;
  return 0;
}

I better stop writing abourt AI now, else this might flame .

But it got THIS part completely WRONG, in this case.

C++'s std::string: It relies on the programmer to manage memory explicitly.

And it's important to the question, since it's the difficulties of doing dynamic memory allocation on a system with only ~2k of memory that causes std:: to be unimplemented for AVR, and also causes the problems with the arduino String class.

1 Like

Ahah, that explains, thanks. Oh well, char it is ...

Nah, I only meant to show where Bard didn't get it right.

AI can be tricky because it's often wrong while having the appearance of confidence. It reminds me of a sales guy at my company.

1 Like

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