Jump to content
  • Advertisement
Sign in to follow this  
deus.ex.nova

Class Member Declarations and Memory

This topic is 2642 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 smile.gif

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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);
}
};

Share this post


Link to post
Share on other sites
If you aren't actually running out of memory, then don't worry about it. Four bytes here and there is nothing.

Share this post


Link to post
Share on other sites
I guess that's true. I've been getting so wrapped up on learning good memory management practices that I lost sight of some fundamental things - like the sizes of data types. lol

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!