Hey Everyone,
I was in the middle of making a fairly large class, and it got me thinking: Does having a lot of declared variables in a class mean that an instance of it will allocate more memory?
In other words, I have a fairly large number (or what I think is large) of declared variables in my class. They are there to store information that is calculated from some of my member functions. What I am wondering is whether or not memory for those variables are immediately allocated when I declare an instance of a class, like "Sphere earth;" or "Sphere earth = new Sphere();". I know that ones initialized in the constructor are, but I'm not sure about the ones that are uninitialized until I call their respective function.
Also, is there any way for me to terminate ("uninitialize") a class variable so that it doesn't use up memory while it's unneeded? Is using pointers the only way to do this?
~Thanks guys
Class Member Declarations and Memory
A class is nothing more but a block of memory. So when you create an instance of that class it is actually allocated enough memory for the entire instance, even if you have not called any of his member functions yet.
Yes all the variables will take up memory even if they are uninitialized.
I'm not sure what language you are using but Sphere earth will take up some memory because it's practically a pointer. When you call new Sphere() more memory is needed to store the new Sphere object.
I'm not sure what language you are using but Sphere earth will take up some memory because it's practically a pointer. When you call new Sphere() more memory is needed to store the new Sphere object.
A class is nothing more but a block of memory. So when you create an instance of that class it is actually allocated enough memory for the entire instance, even if you have not called any of his member functions yet.
I see. Darn...lol
Yes all the variables will take up memory even if they are uninitialized.
I'm not sure what language you are using but Sphere earth will take up some memory because it's practically a pointer. When you call new Sphere() more memory is needed to store the new Sphere object.
I'm using C++. Sorry, I forgot to indicate that in the title.
Thanks, guys for the prompt answers.
Yes, class member variables take up memory right away. It's not very much though, so I wouldn't worry about it. The exception is if you for example make an array of other classes.
// char is 1 byte
char c;
// int is usually 4 bytes
int i;
// This class will be 200 bytes, which is nothing to worry about, and that's with 50 variables
class A {
int member1;
int member2;
...
...
int member50;
};
// This class will take 100 * sizeof(A) = 20 kb, and you might want to use an std::vector instead of a static array
class B {
A members[100];
};
// This class will only take a few bytes of memory until the function is called, at which time 20 kb will be allocated with 'new'
class C {
std::vector<A> members;
void fill() {
members.resize(100);
}
};
If you aren't actually running out of memory, then don't worry about it. Four bytes here and there is nothing.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement