0

I've been reading so much about memory alignment and I didn't get it, I know that's an important method that you have to keep in mind to write efficient code. But I got confused.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
NrBanMex
  • 305
  • 2
  • 8

1 Answers1

0

Basically, CPUs read data from memory a "word" at a time, word being the natural bit-ness of the CPU. If a multi-byte value is not aligned on a word boundary, the CPU has to issue two (or more) reads to get the value, and that is less efficient. Same thing with writing.

  • 2
    That's an oversimplification. Some processors require larger data types to be more restrictively aligned than on a word boundary. It's not uncommon for a `double` to be more restrictively aligned than an `int`, where an `int` is regarded as the "word size". – Tom Karzes Aug 19 '20 at 08:45
  • @TomKarzes: Yes, it is absolutely an oversimplification. – 500 - Internal Server Error Aug 19 '20 at 08:46