Hello everybody.
In my current project, I have problems or fears that the memory of my arduino will run full on a permanent basis.
I'm working on a class for file operations. In the course, I noticed that I apparently handed over a few objects "by value" and not "by reference". The terms comes from C # but should make clear what I mean.
So I read a bit about pointers and references. however, it is not clear to me yet. :O(
There is the normal assignment or transfer of parameters. Then you can explicitly request a parameter with "&" as a pointer (reference) and there is still ""! With "" I have the most understanding problems. And "&" cant be used as "returning the pointer".
I wrote a few small functions. Can someone explain for me how many string copies are in memory? Also by the return's!
String createString(){ String text = "Hi World!"; return text; }
String functionA(String text){ text += " length: " + (String)text.length() + "!"; return text; }
String functionB(String &text){ text += " length: " + (String)text.length() + "!"; return text; }
String textA1 = createString();
String textB1 = createString();
String textA2 = functionA(textA1);
String textB2 = functionB(textB1);
Serial.println("textA1: " + textA1);
Serial.println("textB1: " + textB1);
Serial.println("textA2: " + textA2);
Serial.println("textB2: " + textB2);
textA1: Hi World!
textB1: Hi World! length: 9!
textA2: Hi World! length: 9!
textB2: Hi World! length: 9!
In my understanding:
createString() creates one String and return's a copy? So 2 Strings in Ram and 1 will be deleted after functions ends?
functionA() gets one String Copy, modifies it (new Copy), returns one Copy, so 3 Strings in Ram and 2 will be deleted after functions ends?
functionB() gets one String Reference, modifies it, returns one Copy, so 2 Strings in Ram and 1 will be deleted after functions ends?
imho "deleted" means cleaned by garbage collector.