What are the issues with String variables against using them?

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 (14.3 KB)

CString.h (7.18 KB)

boylesg: How big is the heap in Arduino?

Which Arduino? On an UNO you get 2kb of SRAM and the stack and heap have to share it.

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 - char arrays terminated with 0 - you are entirely in charge of memory usage.

...R

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.

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.

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.

#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

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.

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.

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

How so? Most of them are dirt simple and more powerful than the associated String functions. It's just a different command word and it's not OO but it's definitely not any more of a pain.

The only one String makes easier IMHO is strcat becomes +=. But that is Themistocles evil of all the String functions and not worth the pain it can bring to save what amounts to a few keystrokes.