419
average c++ dev (programming.dev)

I don't think that casting a range of bits as some other arbitrary type "is a bug nobody sees coming".

C++ compilers also warn you that this is likely an issue and will fail to compile if configured to do so. But it will let you do it if you really want to.

That's why I love C++

top 50 comments
sorted by: hot top controversial new old
[-] Contramuffin@lemmy.world 91 points 1 week ago

What do you mean I'm not supposed to add 0x5f3759df to a float casted as a long, bitshifted right by 1?

[-] panda_abyss@lemmy.ca 91 points 1 week ago

I actually do like that C/C++ let you do this stuff.

Sometimes it's nice to acknowledge that I'm writing software for a computer and it's all just bytes. Sometimes I don't really want to wrestle with the ivory tower of abstract type theory mixed with vague compiler errors, I just want to allocate a block of memory and apply a minimal set rules on top.

[-] LodeMike@lemmy.today 8 points 1 week ago

People just think that applying arbitrary rules somehow makes software magically more secure, like with rust, as if the compiler won't just "let you" do the exact same fucking thing if you type the unsafe keyword

[-] BatmanAoD@programming.dev 25 points 1 week ago

It's neither arbitrary nor magic; it's math. And unsafe doesn't disable the type system, it just lets you dereference raw pointers.

[-] Speiser0@feddit.org 12 points 1 week ago

You don't even need unsafe, you can just take user input and execute it in a shell and rust will let you do it. Totally insecure!

[-] ignotum@lemmy.world 14 points 1 week ago

Rust isn't memory safe because you can invoke another program that isn't memory safe?

[-] Speiser0@feddit.org 8 points 1 week ago

My comment is sarcastic, obviously. The argument Kairos gave is similar to this. You can still introduce vulnerabilities. The issue is normally that you introduce them accidentally. Rust gives you safety, but does not put your code into a sandbox. It looked to me like they weren't aware of this difference.

load more comments (3 replies)
[-] jkercher@programming.dev 5 points 1 week ago

100%. In my opinion, the whole "build your program around your model of the world" mantra has caused more harm than good. Lots of "best practices" seem to be accepted without any qualitative measurement to prove it's actually better. I want to think it's just the growing pains of a young field.

[-] eager_eagle@lemmy.world 58 points 1 week ago

C++: all the footguns you need plus a lot more that you never imagined in a single language

[-] magic_lobster_party@fedia.io 56 points 1 week ago

There are no medals waiting for you by writing overly clever code. Trust me, I’ve tried. There’s no pride. Only pain.

[-] Chrobin@discuss.tchncs.de 26 points 1 week ago

It really depends on your field. I'm doing my master's thesis in HPC, and there, clever programming is really worth it.

[-] magic_lobster_party@fedia.io 14 points 1 week ago

Well as long you know what you’re doing and weigh the risks with the benefits you’re probably ok.

In my experience in the industry, there’s little benefit in pretending you’re John Carmack writing fast inverse square root. Understanding what you wrote 6 months ago outweighs most else.

[-] MonkderVierte@lemmy.zip 7 points 1 week ago

Clever as in elegantly and readable or clever as in a hack that abuses a bug/feature and you need to understand the intricacies to understand half of it?

[-] Chrobin@discuss.tchncs.de 12 points 1 week ago

Honestly, also the latter. If you are using hundreds of thousands of cores for over 100h, every single second counts.

[-] merc@sh.itjust.works 8 points 1 week ago

Not only that, but everyone who sees that code later is going to waste so much time trying to understand it. That includes future you.

[-] Zacryon@feddit.org 5 points 1 week ago

That what comments and documentation are for.

[-] merc@sh.itjust.works 8 points 1 week ago

A yes, comments.

int flubTheWozat(void *) {
  for (int i=0; i<4; i++) {
    lfens += thzn[i] % ugy;  // take mod of thnz[i] with ugy and add to lefens.
  }
  return (lfens % thzn[0]) == 4; // return if it's 4ish
}
load more comments (1 replies)
load more comments (3 replies)
[-] merc@sh.itjust.works 44 points 1 week ago

"C++ compilers also warn you..."

Ok, quick question here for people who work in C++ with other people (not personal projects). How many warnings does the code produce when it's compiled?

I've written a little bit of C++ decades ago, and since then I've worked alongside devs who worked on C++ projects. I've never seen a codebase that didn't produce hundreds if not thousands of lines of warnings when compiling.

[-] Zacryon@feddit.org 25 points 1 week ago

I mostly see warnings when compiling source code of other projects. If you get a warning as a dev, it's your responsibility to deal with it. But also your risk, if you don't. I made it a habit to fix every warning in my own projects. For prototyping I might ignore them temporarily. Some types of warnings are unavoidable sometimes.

If you want to make yourself not ignore warnings, you can compile with -Werror if using GCC/G++ to make the compiler a pedantic asshole that doesn't compile until you fix every fucking warning. Not advisable for drafting code, but definitely if you want to ship it.

load more comments (1 replies)
[-] jkercher@programming.dev 19 points 1 week ago

You shouldn't have any warnings. They can be totally benign, but when you get used to seeing warnings, you will not see the one that does matter.

[-] merc@sh.itjust.works 6 points 1 week ago

I know, that's why it bothered me that it seemed to be "policy" to just ignore them.

[-] Jankatarch@lemmy.world 18 points 1 week ago

I put -Werror at the end of my makefile cflags so it actually treats warnings as errors now.

[-] nroth@lemmy.world 13 points 1 week ago

0 in our case, but we are pretty strict. Same at the first place I worked too. Big tech companies.

[-] dejected_warp_core@lemmy.world 10 points 1 week ago

Ideally? Zero. I'm sure some teams require "warnings as errors" as a compiler setting for all work to pass muster.

In reality, there's going to be odd corner-cases where some non-type-safe stuff is needed, which will make your compiler unhappy. I've seen this a bunch in 3rd party library headers, sadly. So it ultimately doesn't matter how good my code is.

There's also a shedload of legacy things going on a lot of the time, like having to just let all warnings through because of the handful of places that will never be warning free. IMO its a way better practice to turn a warning off for a specific line.. Sad thing is, it's newer than C++ itself and is implementation dependent, so it probably doesn't get used as much.

load more comments (2 replies)
[-] Croquette@sh.itjust.works 6 points 1 week ago

A production code should never have any warning left. This is a simple rule that will save a lot of headaches.

[-] Ajen@sh.itjust.works 6 points 1 week ago

My team uses the -Werror flag, so our code won't compile if there are any warnings at all.

load more comments (7 replies)
[-] pennomi@lemmy.world 37 points 1 week ago

C++ is kinky that way. You can consent to all manner of depraved programming patterns. Great for use in personal life, but maybe not appropriate for the office.

[-] Korne127@lemmy.world 53 points 1 week ago

But does it have cargo-mommy :P

[-] qaz@lemmy.world 7 points 1 week ago* (last edited 1 week ago)

https://github.com/Shadlock0133/cargo-vibe

I thought it was a joke, but this is actually viable and even configurable

By default, cargo-vibe will, on success, vibe full strength for 3 seconds.

You can change that by setting CARGO_VIBE_PATTERN environment variable. For example, to set it vibe for 1.5 second on 20% strength, you can do:

CARGO_VIBE_PATTERN="0.2 1.5s" cargo vibe <cmd>

You can also set full patterns of vibes to run, by separating them with slashes /. Here is one example:

CARGO_VIBE_PATTERN="0.4 1s/0.6 1s/0.8 0.75s/1.0 0.25s"

Wait, there's more! https://github.com/funkeleinhorn/cargo-shock

To let Cargo Shock trigger your shock collar use: cargo shock build

To use it everytime you can alias cargo="cargo shock".

Cargo Shock can also be combined with other tools like Cargo Mommy and Cargo Vibe like this: cargo mommy vibe shock build ...

And they have a really slick site: https://openshock.org/

[-] dejected_warp_core@lemmy.world 5 points 1 week ago

TIL there's more than one kind of "vibe" coding.

load more comments (1 replies)
[-] BigDanishGuy@sh.itjust.works 33 points 1 week ago

But it will let you do it if you really want to.

Now, I've seen this a couple of times in this post. The idea that the compiler will let you do anything is so bizarre to me. It's not a matter of being allowed by the software to do anything. The software will do what you goddamn tell it to do, or it gets replaced.

WE'RE the humans, we're not asking some silicon diodes for permission. What the actual fuck?!? We created the fucking thing to do our bidding, and now we're all oh pwueez mr computer sir, may I have another ADC EAX, R13? FUCK THAT! Either the computer performs like the tool it is, or it goes the way of broken hammers and lawnmowers!

[-] daniskarma@lemmy.dbzer0.com 13 points 1 week ago

I will botton for my rust compiler, I'm not going to argue with it.

[-] AnyOldName3@lemmy.world 11 points 1 week ago

Soldiers are supposed to question potentially-illegal orders and refuse to execute them if their commanding officer can't give a good reason why they're justified. Being in charge doesn't mean you're infallible, and there are plenty of mistakes programmers make that the compiler can detect.

load more comments (1 replies)
[-] WhyJiffie@sh.itjust.works 9 points 1 week ago

when life gives you restrictive compilers, don't request permission from them! make life take the compilers back! Get mad! I don’t want your damn restrictive compilers, what the hell am I supposed to do with these? Demand to see life’s manager! Make life rue the day it thought it could give BigDanishGuy restrictive compilers! Do you know who I am? I’m the man who’s gonna burn your house down! With the compilers! I’m gonna get my engineers to invent a combustible compiler that burns your house down!

[-] mormegil@programming.dev 7 points 1 week ago

I understand the idea. But many people have hugely mistaken beliefs about what the C[++] languages are and how they work. When you write ADC EAX, R13 in assembly, that's it. But C is not a "portable assembler"! It has its own complicated logic. You might think that by writing ++i, you are writing just some INC [i] ot whatnot. You are not. To make a silly example, writing int i=INT_MAX; ++i; you are not telling the compiler to produce INT_MIN. You are just telling it complete nonsense. And it would be better if the compiler "prevented" you from doing it, forcing you to explain yourself better.

load more comments (1 replies)
load more comments (4 replies)
[-] wer2@lemmy.zip 32 points 1 week ago

My issue is C++ will "let me do it", and by that I mean "you didn't cast here (which is UB), so I will optimize out a null check later, and then segfault in a random location"

load more comments (1 replies)
[-] Gobbel2000@programming.dev 23 points 1 week ago

I'm all for having the ability to do these shenanigans in principle, but prefer if they are guarded in an unsafe block.

[-] who@feddit.org 20 points 1 week ago

Thank you for including the text as text.

[-] Treczoks@lemmy.world 19 points 1 week ago

Structs with union members that allow the same place in memory to be accessed either word-wise, byte-wise, or even bit-wise are a god-sent for everyone who needs to access IO-spaces, and I'm happy my C-compiler lets me do it.

load more comments (7 replies)
[-] dragonlobster@programming.dev 8 points 1 week ago

No need to cast as any types at all just work with bits directly /s

[-] LillyPip@lemmy.ca 8 points 1 week ago* (last edited 1 week ago)

Why use a strongly typed language at all, then?

Sounds unnecessarily restrictive, right? Just cast whatever as whatever and let future devs sort it out.

$myConstant = ‘15’;
$myOtherConstant = getDateTime();
$buggyShit = $myConstant + $myOtherConstant;

Fuck everyone who comes after me for the next 20 years.

load more comments
view more: next ›
this post was submitted on 22 Jul 2025
419 points (100.0% liked)

Programmer Humor

25407 readers
1697 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 2 years ago
MODERATORS