Argument Data Stored in RAM or Program Memory

Hi,

I am trying to save RAM (dynamic memory) on rather large interface program for a m238p.
This makes me wonder whether arguments to functions are stored in program storage space with the rest of the function or in dynamic memory.

For example,
Which of the following uses less RAM?

this:

data = DataStorageClass(32.0f); % Say each instance of this class contains a couple floats and other bits of large data

void setup(){
    doThingsWithThatData(data);
}
...

or this:

void setup(){
    doThingsWithThatData(DataStorageClass(32.0f));
}
...

Or are they both stored in the same place. Would the same be true if I were just using floats directly as in:

float x = 1.0f;

void setup(){
    doThingsWithData(x);
}
...

vs

void setup(){
    doThingsWithData(1.0f);
}
...

Thanks!
-Connor

If you create an instance of the class as an argument, the instance is created the same way as any other instance - in SRAM. SRAM consists of 2 parts - the heap and the stack. The stack grows from one end of SRAM. The heap grows from the other. When they pass each other, bad things happen.

Passing the object requires that it be copied to the stack. Passing a pointer to the object does not. Only the pointer is copied. If memory is an issue, using new and getting a pointer to the instance of the class, and passing that pointer around is a better idea.

The same applies to non-class objects, except that using new and getting a pointer are usually not recommended. A two byte pointer doesn't save much stack space over a 4 byte float.

Great! Thank you. I hadn't considered the memory saving effects of pointers before but, now that you mention it, it seems like a no-brainer.

Don't forget that references exist too...