this post was submitted on 25 Mar 2024
540 points (98.6% liked)

Programmer Humor

19589 readers
465 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 1 year ago
MODERATORS
 
top 50 comments
sorted by: hot top controversial new old
[–] [email protected] 97 points 7 months ago (2 children)

It makes more sense if you think of const as "read-only". Volatile just means the compiler can't make the assumption that the compiler is the only thing that can modify the variable. A const volatile variable can return different results when read different times.

[–] [email protected] 10 points 7 months ago* (last edited 7 months ago) (3 children)

I thought of it more in terms of changing constants (by casting the const away). AFAIK when it's not volatile, the compiler can place it into read-only data segment or make it a part of some other data, etc. So, technically, changing a const volatile would be less of a UB compared to changing a regular const (?)

[–] [email protected] 66 points 7 months ago (1 children)

const volatile is used a lot when doing HW programming. Const will prevent your code from editing it and volatile prevents the compiler from making assumptions. For example reading from a read only MMIO region. Hardware might change the value hence volatile but you can't because it's read only so marking it as const allows the compiler to catch it instead of allowing you to try and fail.

[–] [email protected] 27 points 7 months ago (1 children)

I will not tell my kids regular scary stories. I will tell them about embedded systems

[–] [email protected] 22 points 7 months ago* (last edited 7 months ago) (2 children)

When you program embedded you'll also dereference NULL pointers at some point.

More...Some platforms can have something interesting at memory address 0x0 (it's often NULL in C).

[–] [email protected] 14 points 7 months ago

In amd64/x86 kernel space you can dereference null as well. My hobby kernel keeps critical kernel structures there XD.

[–] [email protected] 2 points 7 months ago* (last edited 7 months ago)

I was thinking about telling them how in embedded systems it's a good practice to allocate the memory by hand, having in mind the backlog, but yours will come first

[–] [email protected] 19 points 7 months ago* (last edited 7 months ago)

AFAIK when it’s not volatile, the compiler can place it into read-only data segment

True, but preventing that is merely a side effect of the volatile qualifier when applied to any random variable. The reason for volatile's existence is that some memory is changed by the underlying hardware, or by an external process, or by the act of accessing it.

The qualifier was a necessary addition to C in order to support such cases, which you might not encounter if you mainly deal with application code, but you'll see quite a bit in domains like hardware drivers and embedded systems.

A const volatile variable is simply one of these that doesn't accept explicit writes. A sensor output, for example.

[–] [email protected] 2 points 7 months ago (1 children)

The very notion of "less of a UB" is against the concept of UB. If you have an UB in your program, all guarantees are out of the window.

load more comments (1 replies)
[–] [email protected] 6 points 7 months ago (2 children)

I’ve never really thought about this before, but const volatile value types don’t really make sense, do they? const volatile pointers make sense, since const pointers can point to non-const values, but const values are typically placed in read-only memory, in which case the volatile is kind of meaningless, no?

[–] [email protected] 23 points 7 months ago* (last edited 7 months ago) (1 children)

They do in embedded when you are polling a read only register. The cpu can change the register but writing to it does nothing.

load more comments (1 replies)
[–] [email protected] 4 points 7 months ago

Maybe there's a signal handler or some other outside force that knows where that variable lives on the stack (maybe through DWARF) and can pause your program to modify it asynchronously. Very niche. More practical is purely to inhibit certain compiler optimizations.

[–] [email protected] 83 points 7 months ago (4 children)

Some people hate that C is dangerous, but personally I like its can-do attitude.

“Hey C, can I write over the main function at runtime?”

Sure, if you want to, just disable memory protection and memcpy whatever you want there! I trust you.

It’s a great attitude for a computer to have.

[–] [email protected] 69 points 7 months ago (2 children)

C is dangerous like your uncle who drinks and smokes. Y'wanna make a weedwhacker-powered skateboard? Bitchin'! Nail that fucker on there good, she'll be right. Get a bunch of C folks together and they'll avoid all the stupid easy ways to kill somebody, in service to building something properly dangerous. They'll raise the stakes from "accident" to "disaster." Whether or not it works, it's gonna blow people away.

C++ is dangerous like a quiet librarian who knows exactly which forbidden tomes you're looking for. He and his... associates... will gladly share all the dark magic you know how to ask about. They'll assure you, oh no no no, the power cosmic would never pull someone inside-out, without sufficient warning. They don't question why a loving god would allow the powers you crave. They will show you which runes to carve, and then, they will hand you the knife.

[–] [email protected] 21 points 7 months ago

You have a talent for metaphor.

[–] [email protected] 3 points 7 months ago* (last edited 7 months ago) (1 children)

Rust is like a paranoid overprotective guardian. A "mom friend", of sorts. Always the designated driver of the group, keeps you from staying up too late, stops you from eating things that might be choking hazards without proper precaution, and so on and so forth. You'll never meet a person more concerned with your health and safety -- until, that is, you say the magic word "unsafe". Suddenly the alter ego that their hypnotist implanted gets activated, and their entire demeanor changes on a dime. BMX biking? Bungee jumping? Inline assembly? Sounds like a great idea! Let's go, man! Rules are for NERDS! Then the minute the unsafe block ends, they're back to normal, fully cognizant of the adventure they just went on and thinking absolutely nothing of it. "Whitewater rafting with you guys was really fun, especially the part where Jason jumped into the water and I went after him! I'd best go get the first aid kit, though -- that scrape he got when he did that looks like it might get infected. I know he said it didn't hurt, but better safe than sorry!"

They kinda scare you when they're like that, if you're honest.

[–] [email protected] 2 points 7 months ago

I tried thinking of one for Rust, and 'the mom friend with a safeword' is alarmingly accurate.

The secret basement is never locked. It's fine to go down there, alone. You'll only be scarred on the inside.

It's when you go down together that all bets are off.

[–] [email protected] 27 points 7 months ago

Agreed. It's a very adult approach. C hands you a running chainsaw and whatever happens after that is your responsibility. It is also your responsibility to decide when it's not the right time to use C.

[–] [email protected] 21 points 7 months ago (1 children)

This is sometimes practical, too. For example, hooking and extending functions in compiled code that will never be updated by the original author, while preserving the original executable/library files.

[–] [email protected] 8 points 7 months ago (1 children)

You can do that in memory safe languages too. Kotlin extension functions, for example.

[–] [email protected] 16 points 7 months ago* (last edited 7 months ago) (3 children)

Extension functions are not the same at all. Extension functions are syntactic sugar. For example if you have an extension function like

public static class ObjectExtension
{
    public static void DoSomething(this object input) { }
}

You can call that function on an object by doing object.DoSomething() - Yes. But underneath it's the same as doing ObjectExtension.DoSomething(object)

That function does not actually become part of the object, and you can't use it to override existing functions

A closer example of how to do something similar in a memory safe language would be - in C# - using something like Castle DynamicProxy - where through a lot of black magic - you can create a DynamicProxy and fool the CLR into thinking it's talking to an object, while it's actually talking to a DynamicProxy instead. And so then you can actually intercept invocations to existing methods and overrule them

Generally overruling existing functions at runtime is not that easy

[–] [email protected] 5 points 7 months ago (1 children)

Ah my bad, misunderstood the use case.

I thought you were talking about keeping an unmaintained library intact but building onto it.

I thought C was a really dangerous way to use that syntactic sugar pattern. Actual manipulation of the bytecode to maintain and extend a compiled binary is wild

[–] [email protected] 9 points 7 months ago (5 children)

Actual manipulation of the bytecode to maintain and extend a compiled binary is wild

Just wait until you learn about machine code. :)

load more comments (5 replies)
load more comments (2 replies)
[–] [email protected] 11 points 7 months ago

I loved C/C++ in university, finally the damn piece of rock we forced into thinking was doing exactly what I told him to do, no more and no less.

[–] [email protected] 48 points 7 months ago (1 children)

This is actually how you should declare something that you will never change, but something might change externally, like an input pin or status register.

Writing to it might do something completely different or just crash, but you also don't want the compiler getting creative with reads; You don't want the compiler optimizing out a check for a button press because the "constant" value is never changed.

[–] [email protected] 4 points 7 months ago

Yeah I stumbled on this too. Surely the joke should be const mutable, not const volatile.

[–] [email protected] 24 points 7 months ago

If you have a memory-mapped peripheral where there's a readonly register, I could see it being const volatile.

[–] [email protected] 17 points 7 months ago* (last edited 7 months ago)

Just spin the pipe wrench open and slide it up then you can switch it back real quick.

Thank you for watching this OHSA message on bad lockout procedure, now back to your regularly scheduled programming.

[–] [email protected] 17 points 7 months ago (5 children)

What is the context of the original image?

[–] [email protected] 18 points 7 months ago (2 children)

Could be simply a way to make sure the button never moves again. I would have simply taken out the knob, personally.

[–] [email protected] 27 points 7 months ago (1 children)

It could be about sending a message.

A missing knob is easy to fix. Bolting a wrench to the housing holding the knob in place is very explicit. It screams "don't touch"

[–] [email protected] 16 points 7 months ago* (last edited 7 months ago) (1 children)

Idk to me it screams "solve this puzzle and win a free wrench" /s

I like the creativity of it, and it does solve the problem in a way that's user-safe. I thought of removing the knob because that's what I do with my barbecue as I store items on the grill when not in use. Remove knobs, put on grill, close barbecue, cover.

[–] [email protected] 4 points 7 months ago* (last edited 7 months ago)

Idk to me it screams “solve this puzzle and win a free wrench” /s

What too many video games does to a mfer 😄

[–] [email protected] 17 points 7 months ago (1 children)

I work on industrial controls. Very likely that the switch is momentary, meaning it'll go back when released.

Sometimes there's a little piece of plastic in them to remove the momentary setting, but this works too lol. Fuck it, it's maintenance.

load more comments (1 replies)
load more comments (4 replies)
[–] [email protected] 12 points 7 months ago* (last edited 7 months ago) (1 children)

Context is very interesting: https://stackoverflow.com/questions/4592762/difference-between-const-const-volatile

Const flags to the code that you cannot change the value, and volatile flags to the compiler that it's not safe to change the value.

[–] [email protected] 4 points 7 months ago

Volatile means that the value should be read each time its accessed. It can't be cached in a register or the read be otherwise assumed and optimized away or the instructions around its access be reordered.

[–] [email protected] 10 points 7 months ago (1 children)

Is this wrench made of chocolate?

[–] [email protected] 8 points 7 months ago

Forbidden chocolate

[–] [email protected] 10 points 7 months ago (1 children)
[–] [email protected] 8 points 7 months ago (1 children)
[–] [email protected] 2 points 7 months ago

I’m giggling like a kid that finally got the candy from the top drawer. It’s beautiful.

[–] [email protected] 9 points 7 months ago

Looks like they didn't want anybody using the secondary tank. Probably haven't had time to pull Dave's body out yet.

[–] [email protected] 6 points 7 months ago

I've used it in the past when having flash memory blocks that could change but you need the compiler to put them into flash memory and not RAM. It's mainly to get the compiler to stop assuming that it can optimize using the default value.

[–] [email protected] 4 points 7 months ago

laughs in evil PLC programmer A little forces enabled, a change here, and maybe just move this wire over there while I am at it...

[–] [email protected] 3 points 7 months ago

I see a Java programmer evolves into a C programmer

[–] [email protected] 3 points 7 months ago* (last edited 7 months ago)

This has 14 (Peter Cline) energy here for the photo. Keep the dials at zero!

[–] [email protected] 2 points 7 months ago
volatile int blackhole;
blackhole = 1;
const int X = blackhole;
const int Y = blackhole;

Compiler is forbidden to assume that X == 1 would be true. It's also forbidden to assume that X == Y. const just means the address and/or the data at the address is read only. const volatile int* const hwreg; -> "read only volatile value at read only address hwreg". Compiler can assume the hwreg address won't magically change, but can't assume the value read from that address won't.

[–] [email protected] 2 points 7 months ago

When you set the port speed to no negotiate.

load more comments
view more: next ›