0

I am using Arduino.

For text manipulation, there are 2 approaches. First approach is to use an array of characters terminated with null character. This is done in C language. Another way is to use the String object written in C++.

What is the difference between the 2 methods? What are the pros and cons of using String object versus an array of char?

I would like this question to be answered in the context of an embedded system like Arduino.

4
  • I reopened the question because I believe String in Arduino is not the same as std::string. Commented Jan 5, 2016 at 3:36
  • C++ STL gives you byte strings via the std::string object , these aren't the same as strings in other languages. Commented Jan 5, 2016 at 3:40
  • @vsoftco: That said, looks like the Arduino String is still closely related to std::string; it's storing length and capacity, overallocating to minimize reallocations, etc. Commented Jan 5, 2016 at 3:47
  • 1
    @ShadowRanger True, it's definitely completely different from a C-like string, as it is a class similar in flavour to std::string. I just thought the original dupe was not appropriate, and maybe some Arduino experts can clarify on pro/cons. Commented Jan 5, 2016 at 3:49

1 Answer 1

3

As with C++'s std::string, the Arduino String has several advantages:

  1. It can handle strings with embedded NUL bytes ('\0')
  2. Computing the length is constant time (it's a stored value, not derived by scanning for a NUL from the beginning)
  3. It uses an overallocation strategy so repeated in-place concatenation is cheaper; rather than allocating exactly what it needs for the concatenation, it allocates a bit more (implementation dependent) so a bunch of small concatenations don't end up reallocating for every concatenation
  4. It manages a lot of allocation tasks for you as well, making it easier to avoid buffer overflows (where a plain char array is either fixed size, or you're manually managing dynamically allocated memory; either way, the risk of overflow or memory leaks goes up)

The main downsides are that the storage is always dynamically allocated (a stack local char array can reduce memory fragmentation and improve cache coherency), and that you're always storing the length and capacity of the String under the hood, so you're paying (depending on pointer size for the processor and precise guarantees of the type) 12-24 bytes of fixed overhead beyond what a plain stack array of char would involve; if you've got a lot of very small Strings and limited memory, that might hurt you bit (though for a lot of them, even with char arrays you're probably dynamically allocating, so you'd still pay the pointer and allocator overhead, just not the length and capacity overhead).

Essentially, you use String for the same reasons you use std::string in normal C++.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. Upvoted. It seems like it is a bad idea to use String object in Arduino due to memory constraints but good idea to use it on PCs and servers as there are no memory constraints.
@user1824987: If your program is making lots of small strings, yeah, on the 2K-8K of RAM available on an Arduino, wasting 12 bytes or so per string might be a killer. But if your primary function isn't string creation/storage, the benefits in reduced code complexity and improved safety/reliability/speed could well be worth the slight increase in memory overhead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.