0
\$\begingroup\$

In the context of game dev, what is the preferred memory to use to store structs like Entities or MapData which are large in size and have lifetimes lasting the entire game run-time.

For example here is a entity struct I use that is on stack memory. Shall I let it remain on the stack or move it to an Memory Arena I am making to store map data in my game.

typedef struct {
  /*
  The 0th index is reserved for the player and shall not ever turn in the
  entity_empty_slots array.
  */
  SDL_FRect entity_bounding_boxes[ENTITY_POOL_SIZE];
  SDL_Color entity_colors[ENTITY_POOL_SIZE];
  EntityType entity_types[ENTITY_POOL_SIZE];
  float entity_speeds[ENTITY_POOL_SIZE];
  Meter entity_health_meters[ENTITY_POOL_SIZE];
  /*
  Indices which can new entities spawn into.
  This is a stack data structure of the empty indices.
  This follows the LIFO principle.
  */
  IntArr entity_empty_slots;
  /*
  Used to selectively access indices that are occupied
  Used in collision detection using a trick called:
  Sort Sweep and Prune.
  Reference Article: https://leanrada.com/notes/sweep-and-prune/
  */
  IntArr entity_occupied_slots;
} Entities;

Here the ENTITY_POOL_SIZE = 51 and the IntArr is:

typedef struct {
  int32_t arr[ENTITY_POOL_SIZE];
  int32_t len;
}
\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

Large or long-lived data goes on the heap. You want to keep your stack for function execution and temporary values needed along the way.

Allocating too much of your stack to long-term storage leaves you with less room for that stuff, leading to "stack overflow" errors when you try to do just one more thing in a call chain.

Heap allocations have a cost, but for a long-lived item like your entity pool, that's a cost you pay once at startup, rather than every frame/call as you transact with it throughout your game's execution.

\$\endgroup\$
0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.