Get the latest tech news
Writing memory efficient C structs
A struct in C is the best way to organize your data so that you can easily use the data later in your program. However, there are a few caveats to C structures, mainly how their memory works.
Well, dear reader, this padding is added because the CPU needs memory to be aligned in sets of 4 bytes because it’s optimized in that fashion. We can already see that half of the integers potential is unused because the health of a monster should never be negative, so an unsigned int would fit way better. There are still some potential improvements to be made, like you can derive a lot of state from just the name of the Monster, but I’m leaving that up as a challenge for the reader.
Or read this on Hacker News