5
\$\begingroup\$

Why can't I use std::string or any other objects from the C++ Standard Library in my sketches for Arduino? #include <string> doesn't throw an error, so it clearly has no trouble finding the libraries, but actually using anything from it does. If I write

std::string str;

I get

'string' is not a member of 'std'

Why?

\$\endgroup\$
2
  • 1
    \$\begingroup\$ I don't know much about Arduino, but might this discussion help you out? stackoverflow.com/questions/9839990/… \$\endgroup\$ Commented Apr 29, 2013 at 1:05
  • 2
    \$\begingroup\$ C++ and class structurers have a home in applications implemented on big memory processors. The AVRs used on the Arduino just do not have a large enough RAM area to even begin to justify C++ classes and coding styles. \$\endgroup\$ Commented Apr 29, 2013 at 2:31

2 Answers 2

7
\$\begingroup\$

The Arduino supports two kinds of string:

  • C-style character arrays, which the Arduino documentation refers to as strings (lower case 's')
  • String objects, which the the Arduino documentation refers to as Strings (capital 'S')

The later is similar to std::string from the C++ standard library but slightly different. It is an Arduino specific implementation included in the Arduino core library. The Arduino does not use the C++ standard library as it would increase the size of your program too much.

You'll probably find the Arduino String class does what you need. The most significant difference it has from std::string is that is does not support new or delete operators but in most situations this is actually a good thing, new and delete are only used for dynamic (heap based) memory allocations which generally have no place on a 8-bit microcontroller with very limited RAM.

The fact that Arduino does not support new or delete operators does not preclude the use of C++ classes, constructors, etc. It just means that you can not dynamically create and allocate memory to them. This page shows several examples of using the various contructors supported by the Arduino String class. In fact most Arduino librarys are implmented using classes, it's just that when you create instances of these class, they are allocated on the stack or as global variables, rather than dynamically allocated with the new operator.

\$\endgroup\$
1
  • \$\begingroup\$ Okay, so you're the second person to tell me that new and delete aren't supported on Arduino, so I have this question for you. \$\endgroup\$ Commented Apr 29, 2013 at 8:42
5
\$\begingroup\$

Most likely your #include command actually includes the .h file extension:

#include <string.h>

Which gets you the C-style string handling functions defined in string.h. If you omit the file extension:

#include <string>

The output is then is:

Fatal error: string: No such file or directory 
compilation terminated.

As expected for the C++ standard strings, which are not easily supported on Arduino (see the StackOverflow post Kurt E. Clothier mentions )

\$\endgroup\$
6
  • \$\begingroup\$ No, that is not the case. I never write .h when I include a C++ Standard Library header for that exact reason. \$\endgroup\$ Commented Apr 29, 2013 at 1:23
  • \$\begingroup\$ Also, I read that post, and am wondering how one could possibly implement C++ without new or delete operators. That would break constructors and destructors, which stand at the very core of C++ it's self. \$\endgroup\$ Commented Apr 29, 2013 at 1:25
  • \$\begingroup\$ I wonder if you have a string on your Arduino build path containing...??? On your second point, there are discussions on the interwebs about how to implement new and delete: the main problem with this for Arduino is very limited memory (SRAM), and the difficulty of keeping track of non-stack allocated variables, so you wouldn't know when you run out. \$\endgroup\$ Commented Apr 29, 2013 at 1:45
  • 2
    \$\begingroup\$ @MichaelDorst, new and delete are entirely orthogonal to constructors and destructors. You can program C++ entirely with statically allocated (global, static, stack) variables. \$\endgroup\$ Commented Apr 29, 2013 at 3:05
  • \$\begingroup\$ I'm assuming their lack of inclusion is probably more aptly described as "not supported." Like you said, they're language keywords... but they depend on dynamic memory allocation a la malloc/free. If the Arduino IDE is compiling against a stdlib implementation with butchered malloc/free support... well then there you go. \$\endgroup\$ Commented Apr 30, 2013 at 8:25

Not the answer you're looking for? Browse other questions tagged or ask your own question.