0

Is the memory that is allocated to class instances not at all dependent on which member variables are initialized? Will I save memory by creating a new class with less variables if the instances don't use those variable?

For example:

class Animal{
public:
int NumberOfFeet;
int AverageWeight;
};
class Dogs - public Animal  {
public:         
char Breed;
};

int main(){
Animal Snake1;
};

Will I save memory by having two classes as above, rather than having one class with all three different properties?

Dole
  • 339
  • 4
  • 16
  • 2
    It sounds like you're attempting *premature optimization*. First and foremost you should concentrate on making a working, readable and maintainable program. Then if your performance doesn't match the requirements you have (and remember that "good enough" often *is* good enough) then you measure and benchmark and profile to find the hotspots and bottlenecks and memory eaters, and concentrate on those until your performance *is* "good enough", remembering to document and comment the changes you make (since optimizations often leads to bad code). – Some programmer dude Aug 20 '17 at 06:55
  • And for a modern PC with multi-gigabyte amounts of RAM, and for a simple test program or school assignment (unless the assignment *is* about optimizations) then considerations like yours are often not, if ever, needed. – Some programmer dude Aug 20 '17 at 06:57
  • 1
    An object of a class will have the size of all the variables that it can store. If you make an animal object, it will have the two ints. If you make a dog, it will have two ints and a char. If you made a different dog class that just directly had two ints and a char, it would have the same size as the dog in your question. – xaxxon Aug 20 '17 at 07:15

2 Answers2

2
#include <iostream>

using namespace std;

class Animal{
public:
    int NumberOfFeet;
    int AverageWeight;
};
class Dogs : public Animal  {
public:
    char Breed;
};

class Combo{
    int NumberOfFeet;
    int AverageWeight;
    char Breed;

};

int main(){

    Combo combo1;
    Animal Snake1;
    Dogs dog1;

    cout << "Size of combo1: " << sizeof (combo1) << endl;//Size of combo1: 12

    cout << "Size of Snake1: " << sizeof (Snake1) << endl;//Size of Snake1: 8

    cout <<"Size of dog1: " << sizeof (dog1) << endl;//Size of dog1: 12
};

It turned out having one class with all three different properties take less memory in this case at my Xcode IDE.

While it may show different size value depending on different operation system and different alignment setting.

According what I have known, every object should have a size, even though it belong to an empty class, the reason is that every object should have an address in memory so that it can be retrieved later on. If you doubt it, just write code to test it out.

Zhou
  • 21
  • 3
  • Strange... what do you think the reason for this is? Maybe the the class declaration itself takes some space (ie. the variables etc.). – Dole Aug 20 '17 at 09:39
  • As I said every object should have an unique address. [This may help you](https://stackoverflow.com/questions/621616/c-what-is-the-size-of-an-object-of-an-empty-class) – Zhou Aug 20 '17 at 18:47
1

Class is just a wrapper, the size of class is equal to the sum of the size of all its members in common. An exception is that empty class should not have the size of zero.

For your questions:

Is the memory that is allocated to class instances not at all dependent on which member variables are initialized? Will I save memory by creating a new class with less variables if the instances don't use those variable?

No, the compiler will not know whether you use those variables or not. It just a garbage value if you do not use them, but it still a value.

Will I save memory by having two classes as above, rather than having one class with all three different properties?

No, class Dog is inherited from class Animal, thus it will have what Animal has, in this case are NumberOfFeet and AverageWeight and it owns members, in this case is Breed. So the memory is not reduce just because you do not use those two NumberOfFeet and AverageWeight

That is when your two classes depend on the other (Dog depends on Animal). If they are separated (no inheritance), the answer should be yes (since Dog does not contain NumberOfFeet and AverageWeight).

Edited: not all the time the size of class equal to sum of its member variables.

Loi Ly
  • 164
  • 12
  • @Lol Ly Good answer, I will mark it as THE answer... However, I would be creating an animal of the NOT dog sort, and that I believe would save memory as it would have no breed, correct? – Dole Aug 20 '17 at 09:33
  • It is a design problem. I don't know what you are going to do. But in short, the less member variables a class has, the smaller size it will have. For example, if `Dog` class inherited from `Animal`, so array of `Dog` like `Dog arr[100]` would larger than array of `Animal` with the same size. But nowadays, it just a little memory which do not affect much to your performance. `Dog` inherited from `Animal` is more meaningful and logic. – Loi Ly Aug 20 '17 at 10:55