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.

Get the Android app

Or read this on Hacker News

Read more on:

Photo of Memory

Memory

Photo of efficient C structs

efficient C structs

Related news:

News photo

A Photonic SRAM with Embedded XOR Logic for Ultra-Fast In-Memory Computing

News photo

SCP-055 is an "antimeme" – it erases itself from memory when observed

News photo

For Algorithms, Memory Is a Far More Powerful Resource Than Time