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.

guagay_wk
  • 26,337
  • 54
  • 186
  • 295
  • I reopened the question because I believe [`String`](https://www.arduino.cc/en/Reference/StringObject) in Arduino is not the same as [`std::string`](http://en.cppreference.com/w/cpp/string/basic_string). – vsoftco Jan 05 '16 at 03:36
  • C++ STL gives you byte strings via the `std::string` object , these aren't the same as strings in other languages. – Ryan Jan 05 '16 at 03: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. – ShadowRanger Jan 05 '16 at 03: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. – vsoftco Jan 05 '16 at 03:49

1 Answers1

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++.

Community
  • 1
  • 1
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • 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. – guagay_wk Jan 06 '16 at 00:55
  • @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. – ShadowRanger Jan 06 '16 at 02:47