logoalt Hacker News

Everything in C is undefined behavior

474 pointsby lycopodiopsidayesterday at 6:07 AM614 commentsview on HN

Comments

stackedinserteryesterday at 12:31 PM

How can it be valid implementation of isxdigit?

``` int isxdigit(int c) { if (c == EOF) { return false; } return some_array[c]; } ```

If you write code like this, then everything in programming is UB.

my-next-accountyesterday at 7:16 AM

Hello, it's me. I'm not afraid of UB.

show 2 replies
fithisuxyesterday at 7:14 AM

UB can also have impact in logical cohesion of codebase.

synergy20yesterday at 11:10 AM

if c is more ub unsafe than it seems,what is the solution here

crackiyesterday at 7:10 AM

We know. This is not news.

show 1 reply
VimEscapeArtistyesterday at 7:44 AM

Wait until he discovers PowerShell ;D

NooneAtAll3yesterday at 11:26 AM

feels like https://xkcd.com/1499/

the only people complaining about being able to do awful things are people that do awful things

show 1 reply
groby_byesterday at 5:23 PM

"not correctly aligned (probably meaning on an address that’s a multiple of sizeof(int), but who knows)"

I stopped reading there. If you have decades of experience in C/C++ and don't know what that means (and that it's arch specific), I'll assume those decades were mostly the same year over and over.

C/C++ are horrible languages, but they deserve better opponents than that.

SanjayMehtayesterday at 8:47 AM

I used to teach C programming and one time I got anonymous feedback: "when this instructor doesn't know the answer he says "it's compiler dependent.""

Shrug.

jraphyesterday at 6:51 AM

Yet another push to use LLMs after casting fear. Now it should be illegal not to use LLMs. A good start of the day.

(I hope casting fear is not UB)

show 2 replies
pphyschyesterday at 3:01 PM

It's also worth highlighting that C is perhaps the most officially standardized programming language in history.

What a contradiction. Strong evidence that standard-driven programming language development is much worse than implementation-driven development. Standards should be used for data types and external interfaces/protocols, not programming languages.

EGregyesterday at 3:03 PM

a good case can be made that use of C++ is a SOX violation

So Linus was right? But for a second reason too:

C++ is a horrible language. It’s made more horrible by the fact that a lot of substandard programmers use it, to the point where it’s much, much easier to generate total and utter crap with it. Quite frankly, even if the choice of C were to do _nothing_ but keep the C++ programmers out, that in itself would be a huge reason to use C.

That is, accepting C++ code from programmers who use C++ could be a SOX violation ;-)

stackghostyesterday at 6:44 AM

Anyone who uses the construction "C/C++" doesn't write modern C++, and probably isn't very familiar with the recent revisions despite TFA's claims of writing it every day for decades.

Far from being just "C with classes", modern C++ is very different than C. The language is huge and complex, for sure, but nobody is forced to use all of it.

No HN comment can possibly cover all the use cases of C++ but in general, unless you have a very good reason not to:

- eschewing boomer loops in favor of ranges

- using RAII with smart pointers

- move semantics

- using STL containers instead of raw arrays

- borrowing using spans and string views

These things go a long way towards, shall we say, "safe-ish" code without UB. It is not memory-safe enforced at the language level, like Rust, but the upshot is you never need to deal with the Rust community :^)

show 7 replies
JayJSpringpeaceyesterday at 8:13 PM

[flagged]

jim33442yesterday at 7:28 PM

[dead]

creatorsstackyesterday at 4:33 PM

[flagged]

ivandotcodesyesterday at 8:35 AM

[dead]

jdw64yesterday at 6:56 AM

[dead]

tenegoyesterday at 10:57 AM

[flagged]

rahadbhuiyayesterday at 7:39 AM

[dead]

black_13yesterday at 7:39 AM

[dead]

nurettinyesterday at 7:13 AM

[dead]

llggbbttyesterday at 6:50 AM

[flagged]

nokeyayesterday at 6:42 AM

Ok, and?

show 2 replies
Webhixyesterday at 1:04 PM

maybe rewrite this in go?)

benj111yesterday at 9:52 AM

The issue for me with posts like this is that it misses the issue.

Unaligned pointer accesses are UB because different systems handle it differently. This 'should' be to allow the program to be portable by doing what the system normally does.

Instead it's been highjacked by compiler writers, with the logic that "X is UB, therefore can't happen, therefore can be optimised away."

Int c = abs(a) + abs(b); If (a > c) //overflow

Is UB because some system might do overflow differently. In practice every system wraps around.

That should be a valid check, instead it gets optimised away because it 'can't' happen.

C gives you enough rope to hang yourself. The compiler writers don't trust you to use the rope properly.

logicchainsyesterday at 7:10 AM

The concept of undefined behaviour is also a very useful lens for understanding LLM-based coding. Anything you don't explicitly specify is undefined behavior, so if you don't want the LLM to potentially pick a ridiculous implementation for some aspect of an application, make sure to explicitly specify how it should be implemented.

reinhashyesterday at 8:42 AM

Rust.

mbrockyesterday at 7:51 AM

most languages don't even HAVE a specification so in most languages literally EVERYTHING everything is undefined behavior

show 1 reply
grougnaxyesterday at 7:44 AM

Use Rust!

liamd1988yesterday at 7:10 AM

When use C ,keep using char* not mess with int*

momo26yesterday at 7:03 AM

Debugging in C is soooo hard. When I was writing Malloc Lab in system course, there were uncountable undefined and out of range :(

show 1 reply
bullenyesterday at 11:03 AM

Everything in Java is defined behaviour, you need a VM with GC to remain sane.

Everything else is a waste of time!

ricardobeatyesterday at 10:23 AM

I’ve been heavily invested in https://c3-lang.org/ the past couple months. How does it look from this perspective to someone with C experience?

nullpwryesterday at 12:09 PM

Excellent post. But it's addressed to the wrong people.

The problem lies with compilers, not with the language and its specification, or with the creators of the C programming language.

Anyone can write a compiler that transforms all undefined behaviors (UB) into defined behaviors (DB). And your compiler will be used by people, including me.

show 1 reply