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

Programmer Humor

23431 readers
242 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
 
(page 2) 13 comments
sorted by: hot top controversial new old
[–] [email protected] 3 points 1 year ago

I see a Java programmer evolves into a C programmer

[–] [email protected] 83 points 1 year ago (18 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.

load more comments (18 replies)
[–] [email protected] 17 points 1 year ago (5 children)

What is the context of the original image?

[–] [email protected] 18 points 1 year ago (5 children)

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

load more comments (5 replies)
load more comments (4 replies)
[–] [email protected] 10 points 1 year ago (2 children)
load more comments (2 replies)
[–] [email protected] 6 points 1 year 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] 2 points 1 year ago

When you set the port speed to no negotiate.

[–] [email protected] 24 points 1 year ago

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

[–] [email protected] 97 points 1 year ago (5 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 1 year ago* (last edited 1 year ago) (4 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] 19 points 1 year ago* (last edited 1 year 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] 66 points 1 year 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 1 year ago (3 children)

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

load more comments (3 replies)
load more comments (2 replies)
load more comments (4 replies)
load more comments
view more: ‹ prev next ›