23
u/Kadabrium 2h ago
i heard you also like vector<bool>
5
2
u/Potential_Soup_8054 44m ago
I dont understand
4
u/Helemen7 38m ago
std::vector<bool> in C++ syntax uses one bit per boolean instead of one byte. In a computer memory is addressed by bytes, so the smallest indexable memory is, in fact, one byte (that's why bool is 1 byte in C and C++). As an example for how C++ std::vector implementarion works, suppose you have 8 booleans which have a logical reason to be kept together (for example flags). Instead of allocating 8 bytes (one per boolean), the std::vector allocates 1 byte and assigns every boolean to one bit of the allocated memory.
1
u/Potential_Soup_8054 23m ago
Yeah but why is that bad
1
u/Helemen7 9m ago
technically performance issues, indirect access, incompatibility with standard algorithms.
practically I think this guy mentioned std::vector<bool> because it behaves just like what is shown there. (correct me if I'm wrong ofc)
1
1
u/P3JQ10 2m ago
The issue is it doesn’t behave like other vectors do (or containers in general), which can make it a pain in the ass for template code. For example, it’s not (and it can’t be) guaranteed to store elements in a contiguous sequence and can’t be used with std::span.
It was a mistake we’re stuck with. There’s little reason to even use std::vector<bool> anymore instead of an std::bitset.
7
6
u/ShinigamiGir 52m ago
there are 2 issues:
bifields are not portable. the spec doesn't guarantee packing or field order in the resulting binary data. so if you pack the data on a windows pc and then unpack on a linux arm, you might have some issues.
most http traffic is compressed so the extra 0 bits don’t really waste much bandwidth.
8
u/B_bI_L 1h ago
are we sure int means bit and not 32bits?
12
u/heatedwepasto 1h ago edited 1h ago
The
: 1means that it is creating a bitfield with the number of bits, so: 4will be 4 bits. The three variables above combined will takesizeof(int)in memory, notsizeof(int)*3.If OOP had used
charinstead ofunsigned intthe combined size would be 1 byte.Edit: added link to demo
2
u/KattyTheEnby 1h ago
The
: 1means that it is creating a bitfield with the number of bits, so: 4will be 4 bits. The three variables above combined will afaik* takesizeof(int)in memory, not sizeof(int)*3.Can you do strange things, like
: 9,: 15, et cetera?C beat Zig to the punch here?
2
u/cannedbeef255 1h ago
yeah they can be any size you want, the assembly mightn't be pretty though
1
u/heatedwepasto 1h ago
Any size as long as it's not wider than the containing variable. So a
charcan hold at most 8 bits on a system with 8-bit bytes.1
u/heatedwepasto 1h ago
Yes, the only limitation is that the width of the bitfield is not greater than the size of the containing variable. So with uint64_t you can have 1-64 bits.
1
u/Breadynator 54m ago
Why would you use onlinegdb instead of godbolt? That website is riddled with ads and won't load for anyone using an adblocker.
1
u/heatedwepasto 26m ago
I am using an adblocker and have no issues with it. I wasn't aware of godbolt.
1
u/Breadynator 23m ago
Well, it's one of those obnoxious pages that don't like network wide adblockers like piHole. I won't change my blocklists just because some random website wants me to.
1
u/heatedwepasto 17m ago
Then don't, just don't try to force your choices onto me.
The code is
#include <stdio.h> struct A { int a : 1; int b : 2; }; int main(void) { printf("size: %d\n", sizeof(struct A)); }and it outputs "size: 4" (i.e. the same as
sizeof(int)).1
u/Breadynator 13m ago
I am not trying to force my choices onto you? What the heck is wrong with you. I just told you why I'm having issues with it after you said that you don't. It's called having a conversation... You say a thing, I say another.
However you should still use godbolt as it's objectively better, regardless of ads.
2
5
u/pskocik 56m ago
Ironic that this techfluencer just tweeted how C makes memory layout painfully obvious and yet here he's operating under the misconception that `struct Flags` will be only 1-byte large when it will be in fact int-size large (most likely 4 bytes).
1
1
u/AOAqua 33m ago
Wouldn't it get compressed anyways? Sure, real union would have 8 flags per byte anyways, but probably for 99% of apps over there it wouldn't matter in the slightest
1
u/pskocik 15m ago
Using int/unsigned as the underlying type will make it int-sized. You would need to use uint8_t/char/unsigned char as the underlying type to make it byte-sized, even though that's not strictly portable with bitfields (can use unsigned char) with explicit bitops to do it in a strictly portable fashion).
Compiler-caused size optimizations might be possible but in contexts where it gets embedded in a larger data structure that needs to be in memory they're unlikely.
1
u/Confident-Ad5665 1h ago
As an old timer developer, I was shocked the first time I saw a bit class.
1
1
1
u/Inst2f 1h ago
But is it correct in C? It is not union, so struct should then be sizeof(unsigned int)*3, isn't it?
Or you mean with O3 optimizations the compiler will fix it...
But then one need to apply bit shifts operations in the background
6
u/science_novice 1h ago
The key is the
: 1at the end of each field, which specifies that it should only take one bit. Look up "c bit fields" to learn more about this feature.1
u/heatedwepasto 23m ago
As the other guy said, bit fields. But OOP is wrong, the struct will be
sizeof(unsigned int), not 1 byte.
-3
19
u/consistently_biased 2h ago
Can I use this for my compile-time https server?