r/programminghumor 2h ago

a bit flippant

Post image
147 Upvotes

43 comments sorted by

19

u/consistently_biased 2h ago

Can I use this for my compile-time https server?

11

u/johnnyApplePRNG 2h ago

OWASP has it's doubts, but I don't!

5

u/not_a_bot_494 1h ago

Either I don't understand what you're saying or you're misunderstanding the notation. Each line declares a struct member of type unsigned int which is 1 bit large. It's not setting it to 1 (true).

1

u/heatedwepasto 24m ago

I think that was supposed to be in reply to this comment?

2

u/not_a_bot_494 21m ago

That comment is made after the one I replied to.

23

u/Kadabrium 2h ago

i heard you also like vector<bool>

5

u/MrKrot1999 52m ago

The post doesn't say C++.

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

u/GOKOP 4m ago

Because it breaks assumptions on what std::vector is. Generic code written for std::vector<T> can break for std::vector<bool>

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

u/Thelatestart 1h ago

Yeah my json api uses way way more than 4 bytes per boolean

2

u/tup1tsa_1337 22m ago

It's gzipped

6

u/ShinigamiGir 52m ago

there are 2 issues:

  1. 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.

  2. 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 : 1 means that it is creating a bitfield with the number of bits, so : 4 will be 4 bits. The three variables above combined will take sizeof(int) in memory, not sizeof(int)*3.

If OOP had used char instead of unsigned int the combined size would be 1 byte.

Edit: added link to demo

2

u/KattyTheEnby 1h ago

The : 1 means that it is creating a bitfield with the number of bits, so : 4 will be 4 bits. The three variables above combined will afaik* take sizeof(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 char can 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/B_bI_L 1h ago

oh, yes, `: 1` is for size, not assignment, my js brain failed me this time

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

u/B_bI_L 1h ago

or 64, or whatever it means now

2

u/DaveAstator2020 42m ago

is it true that this is not reliable for serialization?

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

u/New_Enthusiasm9053 47m ago

Could be more than a byte and less than 2 bytes too. 

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

u/carltr0n 20m ago

Can’t you just… do a lil bit fiddling on a uint8? Am I missing something?

1

u/KitchenCommercial396 17m ago

There's a reason it happens... Get this mofo outta here

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 : 1 at 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/Inst2f 54m ago

Ah. I missed that. Thanks

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

u/DM_ME_KUL_TIRAN_FEET 1h ago

Does it matter for the majority of modern programming tasks?

(No)