Interesting discovery - Can you figure out what's wrong?

Protoneer:

PeterH:
As an aside, the String class you use in your example can also be problematic since it exposes a heap corruption problem in versions up to 1.0.4 and introduces the risk of heap fragmentation in all versions. In a system with such limited heap space and such limited ability to manage the heap, using dynamic (heap) memory to store dynamic data is not a smart approach. I recommend that you use c-strings (null-terminated char arrays) instead. There are no particular advantages to using the String class and IMO you would be better off forgetting that it exists.

Peter, String has a huge advantages... :slight_smile: It prevents a huge amount of frustration.

The function I am working on will generate GCode commands.. I just want to build simple string but I am finding it hard to even just add a char to char *. :fearful:

Well, adding a char variable to a char string can be trivial if you know exactly where in the string the char variable goes by index. (Either by having an index variable, or knowing a rigid character position structure.) That's because it's just an array that can be accessed just like any other array. (You as the programmer just need to make sure there is a null (0x00, '\0', 0, 00, etc.) at the index position right after the last character of interest before trying to handle the char string as a singular entity.) If you want to put 'r' at the 10th position of char string outputBuffer, it's as simple as outputBuffer[9] = 'r';. If you are continually adding a new character, simply keep track of where you are in the char string with an index variable. Assign the new char at the current index location, increment the index, and insert a null at the new current index location. Wash, rinse, repeat for each new char to be appended to the char string. (Ok, yes, you also need to watch to make sure you don't try to go past the bounds of your char string...)

The C string libraries just make this easier for you as long as you can remember the abbreviation construction of the function names, or have http://www.cplusplus.com/reference/cstring/ bookmarked. (It's the latter for me...)