0

I'm using arduino several month now, And i don't understand when i can use the arduino object of String. I'll explain, I read a lot and the it's look like that this object is a good way to read/write and use strings, But everywhere they say that this object take a lot of RAM. So i made some project, And almost in all of them the String object not work as i expected. When i check the RAM in my projects i see that when my usage of RAM is greater then 65% the string object start to do problems. Because of this i stop use the string object and use only char arrays.

So my question is:

  • How can i know when i can use String from the beginning?
  • there is a way to change from char array to string or vase versa easily (i know this is completely different logic)?
  • How can i avoid the memory problems with the String object?

All this questions come from that the String object is very effective and easy to use and i want to use it, But i'm fear from RAM problems so i'm not using it.

Thank you very much for reading all this, Hope you can help me!

  • I recommend looking at thsi question: arduino.stackexchange.com/questions/19294/… and this answer arduino.stackexchange.com/a/19295/84 with which I fully agree: DO NOT use String. The linked answer provides all (good) reasons for that. – jfpoilpret Jan 15 '17 at 10:06
  • @jfpoilpret this is not a duplicate, I'm trying to understand how can i use string sometime, in all my project i'm using char array and it's very sad there is such a good object for this and i don't know if i can use it at all. – YoavShtainer Jan 15 '17 at 10:52
  • For me the answer to your question is the answer to the above question, then I don't see the point having the same answer here. If you read the answer I linked to, then you shall understand that String usage is generally bad, except if you do simplistic projects and only for fun. I NEVER use String (and never will). – jfpoilpret Jan 15 '17 at 10:55
  • I read the answer, And this is a very good one, thank you for that. According to that, i now sure that is no usage with this object. – YoavShtainer Jan 15 '17 at 10:57
  • I've added an answer to the cited question, which will be relevant here as well, with the understanding that Uno's memory size is borderline to use String objects any more than trivially. – JRobert Jan 15 '17 at 14:49
1

String objects are simple to use. There is a trade-off for simplicity. Strings work by using dynamic memory allocation. Something like this for example:

String foo;
...
foo += 'a';

By adding one character to a string the class has to allocate new memory for the slightly longer string. If you keep doing this you get memory fragmentation.

You are probably better off deciding what your maximum size string is and making that a static array of characters.


So should I use the String objects?

For occasional use they are fine (for example, holding the name of something). They tend to fall down once you start with a small String and keep adding to it, because the code has to dynamically allocate memory, allocating (say) 5 bytes, then 6 bytes, then 7 bytes, and in the process freeing up the original 5 bytes, then 6 bytes and so on. This allocating/freeing process can leave holes in the "heap" causing a thing known as memory fragmentation.

Done to excess you can run out of memory. That is, you might need 100 bytes but have 10 lots of 10 bytes free, with stuff in-between that isn't free.

You don't have lots of memory to start with, so it is probably better, faster and more efficient to decide the largest amount you need for some string, and just make that a character array.

  • so, you said not use the String object? – YoavShtainer Jan 15 '17 at 8:38
  • See amended answer. – Nick Gammon Jan 15 '17 at 21:28

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