C, C++, C#, to name the main ones. And quite a lot of languages are compiled similarly to these.
To be clear, there’s a lot of caveats to the statement, and it depends on architecture as well, but at the end of the day, it’s rare for a byte or bool to be mapped directly to a single byte in memory.
Say, for example, you have this function…
public void Foo()
{
bool someFlag = false;
int counter = 0;
...
}
The someFlag and counter variables are getting allocated on the stack, and (depending on architecture) that probably means each one is aligned to a 32-bit or 64-bit word boundary, since many CPUs require that for whole-word load and store instructions, or only support a stack pointer that increments in whole words. If the function were to have multiple byte or bool variables allocated, it might be able to pack them together, if the CPU supports single-byte load and store instructions, but the next int variable that follows might still need some padding space in front of it, so that it aligns on a word boundary.
A very similar concept applies to most struct and object implementations. A single byte or bool field within a struct or object will likely result in a whole word being allocated, so that other variables and be word-aligned, or so that the whole object meets some optimal word-aligned size. But if you have multiple less-than-a-word fields, they can be packed together. C# does this, for sure, and has some mechanisms by which you can customize field packing.
On ARM it’s ldrb (load register byte), and on RISC-V it’s lb (load byte).
Every decent compiler will turn booleans into words.
No compiler I know of does this. I think you might be getting confused because they’re loaded into registers which are machine-word sized. But in memory a bool is always one byte.
Internally it will still read a whole word. Because the CPU cannot read less than a word. And if you read the ARM article you linked, it literally says so.
Thus any compiler worth their salt will align all byte variables to words for faster memory access. Unless you specifically disable such behaviour. So yeah, RTFM :)
It’s far more often stored in a word, so 32-64 bytes, depending on the target architecture. At least in most languages.
No it isn’t. All statically typed languages I know of use a byte. Which languages store it in an entire 32 bits? That would be unnecessarily wasteful.
C, C++, C#, to name the main ones. And quite a lot of languages are compiled similarly to these.
To be clear, there’s a lot of caveats to the statement, and it depends on architecture as well, but at the end of the day, it’s rare for a
byte
orbool
to be mapped directly to a single byte in memory.Say, for example, you have this function…
The
someFlag
andcounter
variables are getting allocated on the stack, and (depending on architecture) that probably means each one is aligned to a 32-bit or 64-bit word boundary, since many CPUs require that for whole-word load and store instructions, or only support a stack pointer that increments in whole words. If the function were to have multiplebyte
orbool
variables allocated, it might be able to pack them together, if the CPU supports single-byte load and store instructions, but the nextint
variable that follows might still need some padding space in front of it, so that it aligns on a word boundary.A very similar concept applies to most struct and object implementations. A single
byte
orbool
field within a struct or object will likely result in a whole word being allocated, so that other variables and be word-aligned, or so that the whole object meets some optimal word-aligned size. But if you have multiple less-than-a-word fields, they can be packed together. C# does this, for sure, and has some mechanisms by which you can customize field packing.It’s not wasteful, it’s faster. You can’t read one byte, you can only read one word. Every decent compiler will turn booleans into words.
lol what. You can absolutely read one byte: https://godbolt.org/z/TeTch8Yhd
On ARM it’s
ldrb
(load register byte), and on RISC-V it’slb
(load byte).No compiler I know of does this. I think you might be getting confused because they’re loaded into registers which are machine-word sized. But in memory a
bool
is always one byte.Sorry, but you’re very confused here.
You said you can’t read one byte. I showed that you can. Where’s the confusion?
Internally it will still read a whole word. Because the CPU cannot read less than a word. And if you read the ARM article you linked, it literally says so.
Thus any compiler worth their salt will align all byte variables to words for faster memory access. Unless you specifically disable such behaviour. So yeah, RTFM :)
Wrong again. It depends on the CPU. They can absolutely read a single byte and they will do if you’re reading from non-idempotent memory.
If you’re reading from idempotent memory they won’t read a byte or a word. They’ll likely read a whole cache line (usually 64 bytes).
Where?
No they won’t because it isn’t faster. The CPU will read the whole cache line that contains the byte.
Well, I would but no manual says that because it’s wrong!