r/ProgrammerHumor • • 18d ago

Meme cppAndItsVectorBooleans

Post image
3.4k Upvotes

214 comments sorted by

View all comments

Show parent comment

7

u/bwmat 18d ago

Akshually, it has a sizeof of 1, and that could contain a different number of bits, at least prior to one of the newer standard versions

Lol cpp

3

u/Usual_Office_1740 18d ago edited 18d ago

That is what sizeof(bool) outputs. That 1 byte is based on the CHAR_BIT macro which the standard requires is atleast 8 bits. It could also be as much as 32 bits, apparently. Silly C++

4

u/the_horse_gamer 18d ago

this comes from C, which was created at a time where there were processors that used non-8 bit bytes.

there still exist such processors today, used in some niche places.

2

u/KellerKindAs 17d ago

That's the C remnant from a time, where a processor word was not guaranteed to be 8-bit (aligned). Also: CHAR_BIT is atleast 7 bit as a char is required to fit the ASCII table. The 8'th bit is already optional (but guaranteed on all modern systems with 8-bit aligned memory and 32/64 bit cpu words)

0

u/JanB1 18d ago edited 18d ago

Perfectly compilable code in C++23:

bool test1 = true;

int* test2 = reinterpret_cast<int*>(&test1);
*test2 = 5;

std::printf("%d", test1); // Prints "5"

2

u/KuntaStillSingle 18d ago

-1

u/JanB1 18d ago

Okay, not valid because somewhere it's written that "this is undefined behaviour", but compilable, and it does what I wrote. Well, depending on which compiler you use, with some you get an output of 1, with others you get an output of 5. But that's kinda my point.

2

u/No-Information-2571 18d ago

"this is undefined behavior" is a fancy way of saying it might break on a different compiler, platform, or just when used in an unintended way.

Relying on undefined behavior is very bad.

-1

u/JanB1 18d ago

It feels to me that just writing somewhere in some documentation "this is undefined behaviour" is also bad imho, which is one of my biggest grips with C++. So many "undefined behaviour"s that were left that way for "performance", instead of properly addressing/closing them. It's like laying out a minefield for a new programmer, and telling them that the map for the location of the mines is in a hut 300m away from the minefield. And when they step on a mine you scold them and tell them the mine was clearly documented on the map in the hut.

1

u/No-Information-2571 18d ago

What?

No, "undefined behaviour" is a very specific thing, and there's actually not too many of them, and those that are there, have good reasons.

  1. Signed int overflow
  2. Division/modulo by zero
  3. Shift by < 0 or >= type width
  4. Float to int with mismatching type
  5. Null pointer dereference
  6. Out-of-bounds array access
  7. Certain pointer arithmetic
  8. Use after free, double delete
  9. delete mismatch when new/new[]
  10. Delete without virtual destructor
  11. ...

Etc. etc. - and a rather large number you will get warnings in the compiler already. Either way, all of them have tangible reasons.

0

u/vpgel 13d ago

"Actually not too many of them" and yet you outlined at least 10. "Have good reasons" - what reasons?

0

u/No-Information-2571 13d ago

Maybe read the list before asking such a stupid question? Thanks.