Jump to content
  • Advertisement
Sign in to follow this  
ssjvucu

Memory Allocation C++

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

In the turn-based game I am making, combat may or may not occur and the combat will be initialized with different parameters (ex, different attackers/defenders)

My questions is, how costly are the new/delete opererators in C++?

Is example 1 (see below) the "prefered" method, where a new CombatHandler is allocated, processed, and then deleted? Or is example 2 better, where 1 combat handler is created initialized each time it is called, and cleared when the combat is over?

Thanks for the help!

EXAMPLE 1:

class CombatHandler
{
CombatHandler(int arg1, etc...);
};

class PhaseHandler
{
public:
void StartCombat(int arg1, etc...);
void ProcessCombat();
void EndCombat();

private:
CombatHandler* _combat;
};

void PhaseHandler::StartCombat(int arg1, etc...)
{
_combat = new CombatHandler(int arg1, etc...);
}

void PhaseHandler::EndCombat()
{
delete _combat; _combat = NULL;
}

EXAMPLE 2:
class CombatHandler
{
public:
CombatHandler();
void Init(int arg1, etc...);
void Reset();
};

class PhaseHandler
{
public:
PhaseHandler();
void StartCombat(int arg1, etc...);
void ProcessCombat();
void EndCombat();

private:
CombatHandler _combat;
};

PhaseHandler::PhaseHandler()
{
_combat = CombatHandler();
}

void PhaseHandler::StartCombat(int arg1, etc...);)
{
_combat.Init(arg1, etc...);
}

void PhaseHandler::EndCombat()
{
_combat.Reset();
}




Share this post


Link to post
Share on other sites
Advertisement
What about #3:

class PhaseHandler
{
public:
void ProcessCombat(int arg1, etc...);

private:
CombatHandler* _combat;
};

void ProcessCombat(int arg1, etc...)
{
CombatHandler combat(arg1, etc...);
combat.fight();
// destructor cleans up
}


Otherwise I would think about exposing the CombatHandler to the code that would otherwise be calling {Start,Process,End}Combat. However, I'd need to understand how the caller interacts with the PhaseHandler, it might not be possible to do it this way.

As for your question, new/delete are expensive, so avoid where possible. However, be aware that the Init()/Reset() pattern makes it hard to form class invariants, which can make it harder to reason about your program and prevent bugs. Strive for RAII, where possible.

Share this post


Link to post
Share on other sites
This bit is important "it depends on YOUR requirements".

I come from a console development background, so dynamically allocating and freeing memory during the game is not ideal - unless you have your own memory manager and can rely on it.

If you're only concerned with PC development...and don't want to make a commercial game then it doesn't matter which route you take as long as it works to your satisfaction.

HTH

Share this post


Link to post
Share on other sites
Hey rip-off,

I do like your #3 idea, however the ProcessCombat() requires some input steps from the users so I don't think I can use it. However, maybe you can see a better setup than I have right now?

I currently use a process stack to handle the steps.

_stateHandler.Push(&cCombatHandler::EndCombat);
_stateHandler.Push(&cCombatHandler::ProcessCombat);
_stateHandler.Push(&cCombatHandler::StartCombat);

And then in the processing function, it waits until the step returns completed.

retVal = _stateHandler->Process();
if(retVal == COMPLETED_STEP)
_stateHandler->Pop();

Hey Greg,

I currently just program for fun/hobby, so in the end it probably doesn't matter. I do like to learn good code practices from people in the industry like yourself.

Thanks again.

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!