Skip to main content
gamedev.net
🔒 Locked

Virtual functions

Started by Rasmadrak Aug 27, 2005 at 3:23 AM 3 replies 450+ views
Original Post
Rasmadrak
Rasmadrak
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...
"Game Maker For Life, probably never professional thou." =)
ToohrVyk
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.
Rasmadrak
Rasmadrak
Thank you very much! :)

"Game Maker For Life, probably never professional thou." =)
Troll
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.
MaulingMonkey
MaulingMonkey
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.