memory size of array struct much higher than expected

Hi,

I'm making a "larger" platformer game on a Gamebuino META (32 bit Arduino Zero). Now, that I'm finalizing the game, I'm trying to get the most game content in memory as possible. I already did a huge amount of optimization throughout the past months, but I find that some things seem to use a lot more ram than I expect.

I allocate everything statically now, because dynamic allocation doesnt get cleaned up = inevitable crash after loading a few levels...

(Note: I use the distance between stack and heap as a rough indication of my ram usage)

Now, I have this Enemy class:

class Enemy : public Actor // (Actor has 0 data members)
{
	const EnemyDefinition* enemy_definition_ptr_ = nullptr;
	uint8_t settings_ = 0; // bit layout: (ssii?hhh: (s)tate, (i)nterval, (h)ealth)
public:
	uint8_t timer = 0;
	
	AnimationPlayer animation_player = AnimationPlayer();
	Vector16 location = Vector16();

	// member functions omitted
};

This adds up to about 14 bytes ? (the Vector16 is 2 uint16_t's)

But when I have a static array of 16 enemies in my "Scene" i get 1615 bytes ram left between stack and heap. When I bring the array down to 6 enemies, i get 1971 bytes distance. (1971 - 1615) / 10 = +-36... that is a LOT more than 14 bytes. (btw, the device starts crashing around 1300 bytes distance, so I dont have much left :slight_smile: )

Now, I know a bit about padding, but is this huge difference only because of that ??

Are there ways to compact this? Because my scene contains a lot of these arrays, and they all take way more than the actual data within...

Use sizeof to get the size of objects, your structures are not packed, probably using more RAM than you think.

P.S. snippets are useless.

I studied struct padding a bit and did some experimenting in my code with sizeof() like you said.

I also discovered that inheriting from Actor costs 4+ bytes per object. I removed that as well, as it was an acceptable loss of cleanliness for 280 bytes.

So, free ram went up 400 bytes in total again (which is a LOT for my situation), and my structs size make more sense now.

thanks for the tip!

thomasvt:
I also discovered that inheriting from Actor costs 4+ bytes per object. I removed that as well, as it was an acceptable loss of cleanliness for 280 bytes.

Probably a vtable pointer for virtual functions. :wink: