Jump to content
  • Advertisement
Sign in to follow this  
martinis_shaken

State Machine - Heap vs Stack

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

I have a state machine in my game that closely follows the LazyFoo one : http://lazyfoo.net/articles/article06/index.php

This means I have a 

BaseState *currentState = NULL;

which is then set via a function that is called each loop and a flag "nextState"

when the next state is not null, a switch is entered where the next state is created as follows:

    switch( nextState )
         {
             case TITLE_STATE:
                 currentState = new TitleState();
                 break;
 
             case INTRO_STATE:
                 currentState = new IntroState();
                 break;

then nextState is set to null again, rinse and repeat.

-----------------

 

Now,

Everything that happens in the game happens from this state. The rendering is 

currentState->render();

same with the logic, event handling, etc.

 

The character, the world map, the level data, EVERYTHING exists inside of these states.

 

My question is this - Does this mean that my whole game will operate from the heap, even though my objects like Character and Level are declared as 

Character character();
Level level();

This would lead me to believe that the Character and Level would be on the stack, but since their parent class is on the heap (currentState->character, and currentState->level), are they too on the heap? If so, would it be prudent to try and redesign this so that all the states are on the stack?

 

 

Thank You

Share this post


Link to post
Share on other sites
Advertisement
In C++, data lives where you tell it to live at initialization time. If you don't ask for your characters or levels to be heap-allocated via new, then they won't be.

Share this post


Link to post
Share on other sites

In C++, data lives where you tell it to live at initialization time. If you don't ask for your characters or levels to be heap-allocated via new, then they won't be.

 

Just to be certain - even though it's BaseState::character,  character will be on the stack if I deem it so? This means that the container class (BaseClass) will be on the heap, but its members will not be unless I new them?

 

Thank you for the response :)

Share this post


Link to post
Share on other sites

If you did not invoke "new" or some other form of heap allocation function then whatever you're accessing is either on the stack, or in some other location in memory (note, static variables are not on the stack most of the time.)

Share this post


Link to post
Share on other sites

If you did not invoke "new" or some other form of heap allocation function then whatever you're accessing is either on the stack, or in some other location in memory (note, static variables are not on the stack most of the time.)

 

Thank you very much. Just wanted to see if I could optimize a little more ^^

Share this post


Link to post
Share on other sites

If you did not invoke "new" or some other form of heap allocation function then whatever you're accessing is either on the stack, or in some other location in memory (note, static variables are not on the stack most of the time.)

 
Thank you very much. Just wanted to see if I could optimize a little more ^^

Optimize WHAT, exactly? Allocating things on the heap vs stack is very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very rarely ever a performance issue, when done correctly.

Character character();
Level level();

Does not do what you expect. Edited by Washu

Share this post


Link to post
Share on other sites
Optimize WHAT, exactly? Allocating things on the heap vs stack is very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very rarely ever a performance issue, when done correctly.
Character character();
Level level();
Does not do what you expect.

 

 

All I was ever told was that heap is significantly more expensive than stack - allocate without a "new" and you usually get stack, "new" it and you get heap.

I just wanted to ensure that if I have a base class that contains all my objects that exists on the heap, then I did not want all of its member objects (such as character and level, etc.) to be on the heap as well. As this would result in my game effectively running 100% from heap if that were the case. That would be worth fixing, from the sounds of some heap vs stack comparison articles.

Edited by martinis_shaken

Share this post


Link to post
Share on other sites

 

If you did not invoke "new" or some other form of heap allocation function then whatever you're accessing is either on the stack, or in some other location in memory (note, static variables are not on the stack most of the time.)

 

Thank you very much. Just wanted to see if I could optimize a little more ^^

 

 

Obviously constantly using new/delete can be a performance issue.  But once allocated the heap memory is not necessarily slower than stack memory.  Its all the same memory in the end.

Share this post


Link to post
Share on other sites


My question is this - Does this mean that my whole game will operate from the heap, even though my objects like Character and Level are declared as 
Character character();
Level level();
This would lead me to believe that the Character and Level would be on the stack, but since their parent class is on the heap (currentState->character, and currentState->level), are they too on the heap? If so, would it be prudent to try and redesign this so that all the states are on the stack?

 

Where are character and level declared? If they're member variables of some state class, then they'll exist on the stack if that state class is on the stack, or on the heap if that state class is on the heap. You're not giving us enough information to say.

Share this post


Link to post
Share on other sites

All I was ever told was that heap is significantly more expensive than stack

You were told wrong. The act of allocation and freeing memory is expensive, but not because the allocated memory comes from the heap.
There are no relevant platforms where stack memory is any different from heap memory (Nintendo DS is long dead)—especially if you are working on Windows, Mac OS X, Android, or iOS, the stack memory and heap memory are exactly the same, just at different addresses.

So the statement that heap memory is necessarily slower than stack memory is absolutely false.
Both are prone to cache misses and both can be used correctly and efficiently or incorrectly and inefficiently. How well it performs is entirely in your hands.


L. Spiro

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!