State Machine - Heap vs Stack

Started by
8 comments, last by L. Spiro 10 years, 7 months ago

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

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.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

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 :)

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

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

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 ^^

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.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

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.

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.


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.

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

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

This topic is closed to new replies.

Advertisement