Go Down

Topic: Memory Efficiency of Classes (Read 653 times) previous topic - next topic

tails_naf

Sep 08, 2012, 02:13 pm Last Edit: Sep 08, 2012, 02:15 pm by tails_naf Reason: 1
Hi,

I've been browsing general C++ forums regarding if it is more memory efficient to use classes, or not, in terms of SRAM usage - but have not found anything.

For example, say I have a sensor class, that has 4 internal variables- 2 of these are initialized by the user, and 2 are internal temporary values used by internal functions.
The class has 3 routines used to set/get/calculate on the values.

Now say in my code I have 4 instances of the sensor class.
Is it true that I will now have 4 * 4 = 16 variables declared by doing this?

The alternative would be to store the 2 'initializer' parameters for each instance in a struct or array, and have the calculation function with it's internal temporary values in the code.
Now this will consume 4 * 2 (for the struct), plus 2 (for the function temporary value) = 10 variables.

So given this, is using classes less memory efficient, or is my 'guesswork' here completely off target.
If anyone has any links or advice on how variables like this are stored, and what will and won't affect my SRAM usage, it would be most helpful.

Thanks in advance.

pYro_65

#1
Sep 08, 2012, 02:34 pm Last Edit: Sep 08, 2012, 02:38 pm by pYro_65 Reason: 1
You can create the data as local/global variables, or wrap them in a class. Makes no difference as to how much memory gets allocated. the data types decide that.

Routines only take up program memory. no mysterious data is created ( virtual methods are a different piece of pie ). Variables in functions are created on the 'stack' not the 'heap' ( sram ).

Code: [Select]
int a;

a = 2 bytes.

Code: [Select]
struct a{
int b;
};

a c;


c = 2 bytes.

only difference is

Code: [Select]
struct d{}; //empty

d e;


e = 1 byte ( c++ requires all objects are addressable, so 1 byte is allocated )


tails_naf

Thanks for the reply, but I think I may not have explained clearly enough -
What I was saying was in the case of multiple instances of a class - you get each variable multiplied by the number of instances.

However, in the case of a function to do the calculation (the same function that would be in the class):
- If is is a normal function outside the class, then any local variables in it area declared once.
- If the function is inside the class, then any local variables are declared by the number of times the class is instantiated.

In the case of 1 instance of a class, there is no difference, but when multiple instances are used, there could be.

Does this sound correct?

pYro_65

Quote
If is is a normal function outside the class, then any local variables in it area declared once.
- If the function is inside the class, then any local variables are declared by the number of times the class is instantiated.


No, the variables created in a function ( class member or standard non-member function ) are created on the stack, they only exist while the function is running. You need recursion to get multiple instances of a function executing at once.

Code: [Select]

struct MyStruct{

 MyStruct(){
   int a;
 }

 void DoStuff(){
   int b;
 }

 int c;
};


'c' is created when an instance is created, 'a' only gets created when the constructor runs, same as 'b' only exists when DoStuff is called.
a & b are destroyed when their creating function goes out of scope ( ends/returns )

tails_naf

Thanks for that pYro - that makes sense.
I was thinking that the class would have declared all variables, including the temporary variables the function uses as part of the class definition - but as you showed that is not necessary.


skyjumper

Pyro, you are right for local variables, but class variables do exist for the life of the class:

class Myclass {

private:

short int a;
short int b;

};

Each time I instantiate this class, 4 bytes are allocated to store a and b. Those 4 byte remain allocated, in sram, until I delete the instance of the class. If the class is not allocated dynamically, then those 4 bytes remain allocated until the class ceases to exist, perhaps if it goes out of scope.

I believe this is what the OP was asking.


pYro_65


Pyro, you are right for local variables, but class variables do exist for the life of the class:


yup, thats what I explained in my post.

Go Up