Where do member functions reside in memory?

Started by
7 comments, last by Baling 23 years, 7 months ago
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.
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

The Great Milenko"Don't stick a pretzel up your ass, it might get stuck in there.""Computer Programming is findding the right wrench to hammer in the correct screw."
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.
Oops, that was me.
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.

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.
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
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.
- Houdini
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.

-Kentamanos

This topic is closed to new replies.

Advertisement