Jump to content
  • Advertisement
Sign in to follow this  
Rasmadrak

Virtual functions

This topic is 4828 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 there! I just checked out virtual functions, and have a question for ya´ll. :) Is the memory allocated when "new" is called...?

class baseobject
{
public:
vertex3f Pos;
int UniqueID,Type,Owner;

virtual void Draw() =0 ;
};

baseobject* Vehicle[MAX_NR_VEHICLES];



class StaticCar: public baseobject
{
public:
wheel *Wheel[4];

void Draw();
};


class PlayerCar : public baseobject
{
public:
wheel *Wheel[4];
vertex3f Path[MAX_NR_SEARCH_STEPS];

void Draw();
};



simply; is the extra memory needed for the path allocated when a PlayerCar is "born" (using new), or does the compiler see this and allocates all memory needed at bootup, i.e for ALL the baseclasses...? Would this virtual approach save memory if you had say, 10000 cars in which only 100 has pathfinding... compared to 10000 cars where all have pathfinding, except that only 100 use it.... It's really noobish, I know, but when you don't know - you don't know! The old way I did things was to have ONE class with all variables in it... but since I use the TaskManager to monitor the memory, I cant really see a large change in consumtion...

Share this post


Link to post
Share on other sites
Advertisement
The philosophy of the C++ language is "don't pay for what you don't use".

As a matter of fact, the memory for an object is only allocated when you create it, and only the strict necessary minimum is allocated. So yes, your approach would save memory.

Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk
The philosophy of the C++ language is "don't pay for what you don't use".

As a matter of fact, the memory for an object is only allocated when you create it, and only the strict necessary minimum is allocated. So yes, your approach would save memory.


That's not quite true. Most compilers I know will not allocate the "strict necessary minimum" amount of memory. They instead will pad structures as necessary so that they are accessed efficiently. In addition, there's a lot of memory allocation behavior that is left as implementation-dependent, so different compilers may show different allocation overhead numbers.

Share this post


Link to post
Share on other sites
Quote:
Original post by Troll
Quote:
Original post by ToohrVyk
The philosophy of the C++ language is "don't pay for what you don't use".

As a matter of fact, the memory for an object is only allocated when you create it, and only the strict necessary minimum is allocated. So yes, your approach would save memory.


That's not quite true. Most compilers I know will not allocate the "strict necessary minimum" amount of memory. They instead will pad structures as necessary so that they are accessed efficiently. In addition, there's a lot of memory allocation behavior that is left as implementation-dependent, so different compilers may show different allocation overhead numbers.


Yes, but is simply allowing for optimizations by the standard. I can't think of a language which would explicitly disallow such optimizations. In most situations, it's the speed you care about, not memory overhead. You can make appropriate adjustments using compiler-specific keywords tweaks and flags if you need to optimize differently.

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.

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!