# What are the issues with String variables against using them?

**URL:** <https://forum.arduino.cc/t/what-are-the-issues-with-string-variables-against-using-them/404523>\
**Category:** General Discussion\
**Created:** [August 24, 2016, 11:44pm UTC](https://forum.arduino.cc/t/what-are-the-issues-with-string-variables-against-using-them/404523 "2016-08-24T23:44:39Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![boylesg](https://avatars.discourse-cdn.com/v4/letter/b/e0b2c6/32.png) [@boylesg](https://forum.arduino.cc/u/boylesg)\
**Post date:** [August 24, 2016, 11:44pm UTC](https://forum.arduino.cc/t/what-are-the-issues-with-string-variables-against-using-them/404523/1 "2016-08-24T23:44:39Z")

</div>

I often hear about people having issues with String variables stuffing up their sketched.

However so far I have not noticed any issues with them in any of my sketches.

I have read about possible heap fragmentation when using malloc and new etc.

How big is the heap in Arduino?

[CString.cpp](https://forum.arduino.cc/uploads/short-url/oeKQrQERMtfZAsjeXviQXsWoJzt.cpp) (14.3 KB)

[CString.h](https://forum.arduino.cc/uploads/short-url/9MIwuzLz9vYFqijrZT2diPcxkR1.h) (7.18 KB)

---

<div class="post-metadata">

**Author:** ![Robin2](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robin2/32/216045_2.png) [@Robin2](https://forum.arduino.cc/u/Robin2)\
**Post date:** [August 25, 2016, 6:47am UTC](https://forum.arduino.cc/t/what-are-the-issues-with-string-variables-against-using-them/404523/3 "2016-08-25T06:47:05Z")

</div>

> boylesg:  
> However so far I have not noticed any issues with them in any of my sketches.

Avoiding the String class is a precautionary measure - so it does not come and bite you when you don't expect it. I have seen a few Threads where the program ran into problems after running for a long time (several hours of a few days).

If you use [cstrings](http://www.cplusplus.com/reference/cstring/) - char arrays terminated with 0 - you are entirely in charge of memory usage.

...R

---

<div class="post-metadata">

**Author:** ![OldSteve](https://avatars.discourse-cdn.com/v4/letter/o/0ea827/32.png) [@OldSteve](https://forum.arduino.cc/u/OldSteve)\
**Post date:** [August 25, 2016, 7:24am UTC](https://forum.arduino.cc/t/what-are-the-issues-with-string-variables-against-using-them/404523/4 "2016-08-25T07:24:52Z")

</div>

Apparently the memory leak issue has been fixed, but fragmentation is still an issue, and the String class also uses an exhorbitant amount of SRAM for even the simplest of tasks. Even an empty String uses 6 bytes.  
Dynamic memory allocation is always something to avoid when working with microcontrollers.  
As you say, new/delete and malloc()/free() are considered taboo unless _absolutely_ necessary.

I'm with Robin - it's well worth taking the effort to learn how to use C strings. The standard C/C++ string manipulation functions are very powerful once you're used to them, and especially so if you use the avr-libc flash memory versions like 'strcpy\_P()' etc.

---

<div class="post-metadata">

**Author:** ![boylesg](https://avatars.discourse-cdn.com/v4/letter/b/e0b2c6/32.png) [@boylesg](https://forum.arduino.cc/u/boylesg)\
**Post date:** [August 25, 2016, 9:53am UTC](https://forum.arduino.cc/t/what-are-the-issues-with-string-variables-against-using-them/404523/5 "2016-08-25T09:53:57Z")

</div>

> OldSteve:  
> Apparently the memory leak issue has been fixed, but fragmentation is still an issue, and the String class also uses an exhorbitant amount of SRAM for even the simplest of tasks. Even an empty String uses 6 bytes.  
> Dynamic memory allocation is always something to avoid when working with microcontrollers.  
> As you say, new/delete and malloc()/free() are considered taboo unless _absolutely_ necessary.
> 
> I'm with Robin - it's well worth taking the effort to learn how to use C strings. The standard C/C++ string manipulation functions are very powerful once you're used to them, and especially so if you use the avr-libc flash memory versions like 'strcpy\_P()' etc.

Well I know how to use c strings and the functions, but it is a real pain the ar$e.

I was not aware of the issue with Strings and sketches running for several hours so thanks.

Lucky I have finished making my own string class, based on PString, but with the similar functionality to Strings.

I can't seem to attach the files to a reply so I will try and attach them to my original post.

If not then I will have to start a new thread and attach them to that instead.

Either way I would appreciate thoughts / opinions on my string class.

---

<div class="post-metadata">

**Author:** ![boylesg](https://avatars.discourse-cdn.com/v4/letter/b/e0b2c6/32.png) [@boylesg](https://forum.arduino.cc/u/boylesg)\
**Post date:** [August 25, 2016, 10:50am UTC](https://forum.arduino.cc/t/what-are-the-issues-with-string-variables-against-using-them/404523/6 "2016-08-25T10:50:04Z")

</div>

Particularly these macros which allow you to do this in your sketch:

DEC\_CSTRING1("surname", 20)  
DEC\_CSTRING2("given name", 20)

CSTRING("surname") = "Boyles";  
CSTRING(given name") = "Greg";

CSTRING1("surname")[1] = 'x';

I am wondering if there is a better/shorter way to do this.

I could shorten the macro names for starters.

```cpp
#define DEC_CSTRING1(STR_NAME, N_SIZE) char* strBuff1[N_SIZE]; CString str1((char*)strBuff1, N_SIZE);
#define CSTRING1(STR_NAME) str1

#define DEC_CSTRING2(STR_NAME, N_SIZE) char* strBuff2[N_SIZE]; CString str2((char*)strBuff2, N_SIZE);
#define CSTRING2(STR_NAME) str2

#define DEC_CSTRING3(STR_NAME, N_SIZE) char* strBuff3[N_SIZE]; CString str3((char*)strBuff3, N_SIZE);
#define CSTRING3(STR_NAME) str3

#define DEC_CSTRING4(STR_NAME, N_SIZE) char* strBuff4[N_SIZE]; CString str4((char*)strBuff4, N_SIZE);
#define CSTRING4(STR_NAME) str4

#define DEC_CSTRING5(STR_NAME, N_SIZE) char* strBuff5[N_SIZE]; CString str5((char*)strBuff5, N_SIZE);
#define CSTRING5(STR_NAME) str5

#define DEC_CSTRING6(STR_NAME, N_SIZE) char* strBuff6[N_SIZE]; CString str6((char*)strBuff6, N_SIZE);
#define CSTRING6(STR_NAME) str6

```

---

<div class="post-metadata">

**Author:** ![OldSteve](https://avatars.discourse-cdn.com/v4/letter/o/0ea827/32.png) [@OldSteve](https://forum.arduino.cc/u/OldSteve)\
**Post date:** [August 25, 2016, 11:08am UTC](https://forum.arduino.cc/t/what-are-the-issues-with-string-variables-against-using-them/404523/7 "2016-08-25T11:08:13Z")

</div>

Well maybe someone will want to spend the time evaluating your modified PString library, but not me. I'm perfectly happy with my current methods, and am very comfortable with directly manipulating C strings.

You need to write some documentation and some examples to illustrate usage of all of it's features, then present it and ask for beta testers.

(I even tried PString once, then deleted it because I found no use for it.)

I'm sure that others will be more eager, if it's presented properly.

---

<div class="post-metadata">

**Author:** ![boylesg](https://avatars.discourse-cdn.com/v4/letter/b/e0b2c6/32.png) [@boylesg](https://forum.arduino.cc/u/boylesg)\
**Post date:** [August 25, 2016, 11:17am UTC](https://forum.arduino.cc/t/what-are-the-issues-with-string-variables-against-using-them/404523/8 "2016-08-25T11:17:46Z")

</div>

> OldSteve:  
> Well maybe someone will want to spend the time evaluating your modified PString library, but not me. I'm perfectly happy with my current methods, and am very comfortable with directly manipulating C strings.
> 
> You need to write some documentation and some examples to illustrate usage of all of it's features, then present it and ask for beta testers.
> 
> (I even tried PString once, then deleted it because I found no use for it.)
> 
> I'm sure that others will be more eager, if it's presented properly.

Most of the functions are identical to the String class.

A few have my own spin of them and there are a few extra functions that I find convenient.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 3:47pm UTC](https://forum.arduino.cc/t/what-are-the-issues-with-string-variables-against-using-them/404523/10 "2021-05-05T15:47:24Z")

</div>

Closed on May 5, 2021, 3:47pm UTC


