It is not. A bool
in C, C++, Rust, Go, and every language that I know is 1 byte. Why are you arguing this basic very well known fact so much?
Just say “oh I was mistaken, TIL”. It’s not shameful.
It is not. A bool
in C, C++, Rust, Go, and every language that I know is 1 byte. Why are you arguing this basic very well known fact so much?
Just say “oh I was mistaken, TIL”. It’s not shameful.
By that measure basically every StackOverflow question asking how to do something is opinion based - the very nature of the site is questions asking for the best solutions. The “opinion based” rules is NOT meant to prevent questions like this. This is the kind of useless pedantry that killed StackOverflow.
I think it comes from a fundamental disconnect. You have something like:
not sure why that wasn’t enough for you
I never said it wasn’t; just that it shouldn’t have been closed.
Just because it’s closed doesn’t mean the answers already provided are bad.
Again, I never said otherwise. The point is it shouldn’t have been closed.
but calling malloc_usable_size(malloc(1)) is giving me 24, so it at least allocated 24 bytes for my 1, plus any tracking overhead
Indeed. Padding exists. A bool is still one byte.
it’ll get 3+ extra bytes added on the next fn call.
…of padding. Jesus. Are you going to claim that uint16_t
is not 2 bytes because it is sometimes followed by padding?
No, in C and C++ a bool is a byte.
since many CPUs require that for whole-word load and store instructions
All modern architectures (ARM, x86 RISC-V) support byte load/store instructions.
or only support a stack pointer that increments in whole words
IIRC the stack pointer is usually incremented in 16-byte units. That’s irrelevant though. If you store a single bool on the stack it would be 1 byte for the bool and 15 bytes of padding.
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
Again, no. I think you’ve sort of heard about this subject but haven’t really understood it.
The requirement is that fields are naturally aligned (up to the machine word size). So a byte needs to be byte-aligned, 2-bytes needs to be 2-byte aligned, etc.
Padding may be inserted to achieve that but that is padding it doesn’t change the size of the actual bool, and it isn’t part of the bool.
But if you have multiple less-than-a-word fields, they can be packed together.
They will be, if it fits the alignment requirements. Create a struct with 8 bools. It will take up 8 bytes no matter what your packing setting is. They even give an example:
If you specify the default packing size, the size of the structure is 8 bytes. The two bytes occupy the first two bytes of memory, because bytes must align on one-byte boundaries.
They used byte
here but it’s the same for bool
because a bool is one byte.
I’m really surprised how common this misconception is.
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).
And if you read the ARM article you linked, it literally says so.
Where?
Thus any compiler worth their salt will align all byte variables to words for faster memory access.
No they won’t because it isn’t faster. The CPU will read the whole cache line that contains the byte.
RTFM
Well, I would but no manual says that because it’s wrong!
but if you have a single bool in a stack frame it’s probably going to be more than a byte.
Nope. - if you can’t read RISC-V assembly, look at these lines
sb a5,-17(s0)
...
sb a5,-18(s0)
...
sb a5,-19(s0)
...
That is it storing the bools in single bytes. Also I only used RISC-V because I’m way more familiar with it than x86, but it will do the same thing.
on the heap definitely more than a byte
Nope, you can happily malloc(1)
and store a bool in it, or malloc(4)
and store 4 bools in it. A bool is 1 byte. Consider this a TIL moment.
You said you can’t read one byte. I showed that you can. Where’s the confusion?
My favourite closed question is how to do Case-insensitive string comparison in C++ - closed as opinion based!
ChatGPT helped, but this is why you died StackOverflow.
I disagree. I still easily find new questions to ask, for example this one which is a nice demonstration of why StackOverflow is dying. Or this one (which also received 4 downvotes).
Even so, I definitely go to ChatGPT first now. Now that we finally have an alternative to the toxic downvotes/closing, why would I go there unless I absolutely need to?
I think he’s talking about if a variable only exists in registers. In which case it is the size of a register. But that’s true of everything that gets put in registers. You wouldn’t say uint16_t
is word-sized because at some point it gets put into a word-sized register. That’s dumb.
things that store it as word size for alignment purposes
Nope. bools only need to be naturally aligned, so 1 byte.
If you do
struct SomeBools {
bool a;
bool b;
bool c;
bool d;
};
its 4 bytes.
You can’t read one byte
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’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.
I’m guessing there’s a reason they wanted min() to be able to be called without any arguments but I’m sure it isn’t a good one.
It not a totally unreasonable definition. For example it preserves nice properties like min(a.concat(b)) == min([min(a), min(b)])
.
Obviously the correct thing to do is to return an optional type, like Rust does. But … yeah I mean considering the other footguns in Javascript (e.g. the insane implicit type coersion) I’d say they didn’t do too badly here.
Completely wrong. I’m hoping this was a joke… :-D
You can’t store data in parity bits… so it’s irrelevant.
It’s not just less memory though - it might also introduce spurious data dependencies, e.g. to store a bit you now need to also read the old value of the byte that it’s in.
I don’t think so. Apart from dynamically typed languages which need to store the type with the value, it’s always 1 byte, and that doesn’t depend on architecture (excluding ancient or exotic architectures) or optimisation flags.
Which language/architecture/flags would not store a bool in 1 byte?
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.
Well there are containers that store booleans in single bits (e.g. std::vector<bool>
- which was famously a big mistake).
But in the general case you don’t want that because it would be slower.
Catchy. Can’t imagine why “algorithm” caught on instead.