Pointers and Objects - how many Objects are stored in Ram?

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.

Don't use the String (capital S) class. No, really... Don't!

imho "deleted" means cleaned by garbage collector

There is no such thing as a garbage collector on AVR's.

AFAICT, "functionA" creates 4 instances and "functionB" creates 3 (because it references "textB1" as "text")

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

...R

Ok, thanks.

The string example should only demonstrate working with objects. I work with file objects, but I did not want to post the whole entire source code.

in detail, I am concerned with how objects are treated in the example above (otherwise think of Object "File" instead of "String").

DrDooom: otherwise think of Object "File" instead of "String".

Would not make any sense, none at all (File + File = Disk?) :) If you want your sketch to be "long term stable", you should reserve whatever memory, objects and what else you need in the setup phase of your sketch. The memory management (or lack thereof) on the Arduino is as crude as it gets, and the usage of dynamic memory during runtime (allocating, deallocating, reallocating) will cause memory fragmentation which may result in lockup or malfunction :)

DrDooom: in detail, I am concerned with how objects are treated in the example above (otherwise think of Object "File" instead of "String").

If you want advice about file objects then please produce an example program using file objects - not some proxy that might not even be a good proxy.

...R