Search code examples
cpu-word32-bitaddressing

Word Addressable Memory


For a 32 bit word addressable memory, the word has size of 4 bytes.

If I try to store a data structure uses less than 4 byte memory, say 2 bytes. Is the remaining 2 bytes wasted?

Should we consider the word size when we decide what data structure to use?

Got similar question here but not exactly what i am asking.

Please help.


Solution

  • On a modern CPU, memory itself is retrieved in usually chunks called cache lines (64 bytes on x86), but the CPU instruction set can address individual bytes.

    If you had some esoteric machine with an instruction set that couldn't address individual bytes, then your compiler would hide that from you.

    Whether or not memory is wasted in data structures smaller than a word would depend on the language you use and its implementation, but generally, records are aligned according to the field with the coarsest requirement. If you have an array of 16 bit integers, they will pack together tightly.