CString
Introduction
Whilst use of char* pointers is very common in Sming code, it is generally advisable to avoid pointers in C++ where possible.
The STL provides class templates such as unique_ptr which deals with memory allocation and de-allocation to avoid issues with memory leaks.
The CString class implements this on a char[] and adds some additional methods which are similar to the String
class.
String vs. CString
String
objects each require a minimum of 24 bytes of RAM, and always contain a length field.
A CString is much simpler and contains only a char* pointer, so a NULL string is only 4 bytes.
When storing arrays or lists of strings (or objects containing those strings) which change infrequently, such as fixed configuration data, use a CString for memory efficiency.
API Documentation
-
class CString : public std::unique_ptr<char[]>
Class to manage a NUL-terminated C-style string When storing persistent strings in RAM the regular String class can become inefficient, so using a regular
char*
can be preferable. This class provides that with additional methods to simplify lifetime management and provide some interoperability with Wiring String objects.