Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Baling

Where do member functions reside in memory?

This topic is 6619 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

Hi guys and gals, I''ve got this question here, after seeing some people actually use an object (class) for each tile on their map: will it take up a lot of memory space? Are member functions of a class resides in memory only when they''re called? Then what does the memory reserved for an object hold? Only class members? Something else? Thanks a lot for your time and perhaps, your answers.

Share this post


Link to post
Share on other sites
Advertisement
well I''m not expert on this but from my understanding there is a single copy of each function that all instances of the class uses the data is the only part that is recreated for each instances... so its not realy any worse then a struct... there might be some extra overhead for virtual members.......

Great Milenko

Words Of Wisdom:
"Never Stick A Pretzel In Your Butt It Might Break Off In There."


http://www.crosswinds.net/~milenko
http://www.crosswinds.net/~pirotech

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
If it''s not a virtual method it''s pretty much same as a regular C function, except it takes an extra argument - pointer to an instance of your class. Virtual functions are a bit more complicated. Every instance of a class has a pointer to what is called "virtual function table" (aka vtable) in addition to your class members. vtable is what it sounds like - an array of pointers to all of your class'' virtual methods. All instances of the same class share a single vtable.

There is only one instance of machine code for every method (unless you inline). There''s some memory overhead - you have to store a vtable. And there''s some CPU overhead - when calling a virtual function you have to dereference a few pointers.

Share this post


Link to post
Share on other sites
Thanks guyz, for your help. So are there anymore to come?

Guess I''ll just leave my elements of map remain as structures. That will give me the most optimal performance.

Share this post


Link to post
Share on other sites
Each instance of a class only duplicates its variables, not its functions. Class functions are no different from ''normal'' functions in that regard. The only change is that the compiler only allows them to be executed with an object of the correct type. So, they take up no more space than if you were just using structs.

Share this post


Link to post
Share on other sites
Kylotan,

But there''s an extra step in referencing the member functions inside class right? So if you were to do this to each of the tiles, slow down might be significant. Tell me if I''m wrong :p

Share this post


Link to post
Share on other sites
Yes, a reference to the class must be passed to the member function (this is all done internally of course). However, doing this in C:

DrawTile(TileStruct *pTile)
{
// draw tile here
}

Is the exact same as creating a tile class that contains a Draw function. They BOTH have to pass a pointer to the draw function. In fact, the compiler would BASICALLY turn your C++ member function into the one stated above. There would be no speed difference. If you have a class that would require only one instance of, then yes, you would have an extra parameter, but no, the slow down would not be significant.

Share this post


Link to post
Share on other sites
Just a comment on the statement from Baling:
"Guess I''ll just leave my elements of map remain as structures. That will give me the most optimal performance."

In C++, there is only ONE difference between a struct and a class. That''s default access rights. Struct defaults to public, and class defaults to private.

FYI: It''s a pretty popular C++ interview question.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!