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.
Asked
Active
Viewed 151 times
0
-
Try wikipedia. https://en.m.wikipedia.org/wiki/Data_structure_alignment – n. m. could be an AI Aug 19 '20 at 08:51
1 Answers
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.

500 - Internal Server Error
- 28,327
- 8
- 59
- 66
-
2That'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