Word Addressable Memory

523 Views Asked by At

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.

2

There are 2 best solutions below

4
Matt Timmermans On BEST ANSWER

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.

2
iggy On

If you have 3 or 4 integers, it scarcely matters whether you store them in 2, 4, or 8 bytes.

If you have 3 or 4 billion integers, then it's probably worth considering a more space-efficient structure.

Generally speaking, the natural integer size for a given language implementation is supposed to be optimal in some way, so my advice is in general 'use int unless you know it's not appropriate' and let the compiler worry about it - until you have performance data to show otherwise.