Live data from Hacker News

“C is how the computer works” is a dangerous mindset for C programmers

words.steveklabnik.com

241–250 of 387 posts

Re: “C is how the computer works” is a dangerous mindset for C programmers

#241
post #6

Actually I look forward for enough momentum to be created in the community, to allow new C standards to change the way undefined behaviors are conceived, and make them as specified as possible, and even when they cannot in reasonable unique ways, to provide reference possible behaviors to select in order to have the least unexpected outcome for the programmer. Especially now that low level programming is abstracting…

It would also be nice if Rust had a specification. Without a spec it's impossible to build a correct alternative Rust implementation.

That's not true, as mrustc proves. The C and C++ specs are incredibly ambiguous and frankly I don't know if they add that much value over good documentation.

Re: “C is how the computer works” is a dangerous mindset for C programmers

#242

Earlier quoted context omitted.

It would also be nice if Rust had a specification. Without a spec it's impossible to build a correct alternative Rust implementation.

Just pains me that the Rust team is so oblivious to the fact that they need a spec if they ever hope to have a chance to replace C.

And yet C, which had no spec at the time, beat out Algol, which had one.

Re: “C is how the computer works” is a dangerous mindset for C programmers

#243

Earlier quoted context omitted.

No. It is not "undefined behavior". The "behavior" here is assignment (reading memory). It always will behave precisely and consistently: it _will_ assign the "indeterminite" value of the un-initialized memory. Using such an "indeterminite value" in other operations (e.g comparison per the link Steve B. posted above: https://www.ralfj.de/blog/2019/07/14/uninit.html ) is the UB bit.

> [reading memory} always will behave precisely and consistently What is the point of not making it UB? You can't AFAICS possibly rely on any useful way on it behaving 'precisely and consistently' so just make it UB anyway?

I think this lets you copy it around, e.g. when you have a struct that you have partially initialized.

Re: “C is how the computer works” is a dangerous mindset for C programmers

#244

Earlier quoted context omitted.

C++ should only be used as a last resort. It's complex, development is slower, the tooling is poor, some universities have stopped teaching it. It's very hard to hire good C++ devs, and they are typically not cheap. I manage a c++ team btw. Our app has to be fast.

What are you comparing it to? I think C++ tooling is fairly decent. (By the way, what do you work on?)

I think java has very good tooling.

Java's main issue is the barrier to entry is lower because it has good tooling, which means really bad, inexperienced programmers can write bad code, and have more tooling to bail them out, allowing them to continue down that path until they have a huge ball of mud.

There ought to be some kind of phrase for this "the tooling curse".

GDB isn't bad, IMHO, but the inability of IDEs to deterministically parse templates / index code without huge problems is problematic.

Re: “C is how the computer works” is a dangerous mindset for C programmers

#245

Earlier quoted context omitted.

> pointer provenance, inability to read uninitialized memory what do you mean by these two? not sure about the former. as for the latter, of course you can read uninitialized memory, unless you mean something else?

Can you write a well-defined C program that reads uninitialised memory?

Reads? I think so; I believe the bad part is when you try to use it.

Re: “C is how the computer works” is a dangerous mindset for C programmers

#247

Earlier quoted context omitted.

What are you comparing it to? I think C++ tooling is fairly decent. (By the way, what do you work on?)

I think java has very good tooling. Java's main issue is the barrier to entry is lower because it has good tooling, which means really bad, inexperienced programmers can write bad code, and have more tooling to bail them out, allowing them to continue down that path until they have a huge ball of mud. There ought to be some kind of phrase for this "the tooling curse". GDB isn't bad, IMHO, but the inability of IDEs to…

> the inability of IDEs to deterministically parse templates / index code without huge problems is problematic.

More like the inability of text editors to parse templates; most IDEs rely on a full compiler backend that can usually figure things out.

Re: “C is how the computer works” is a dangerous mindset for C programmers

#248

Earlier quoted context omitted.

Can you write a well-defined C program that reads uninitialised memory?

Reads? I think so; I believe the bad part is when you try to use it.

No, it is undefined behaviour. Check the spec.

Re: “C is how the computer works” is a dangerous mindset for C programmers

#249
post #91

Earlier quoted context omitted.

>inability to read uninitialized memory printf("%x", *(int*)0x12345678);

Even casting 0x12345678 to int* is undefined behavior

This happens all the time on embedded systems, when you're writing a device driver, etc. Practically speaking, it's well defined what will happen.

Re: “C is how the computer works” is a dangerous mindset for C programmers

#250

One of the blog posts I keep meaning to write is in the vein of "Why C is not portable assembly." Ironically, most of my points about C failures aren't related to UB at all, but rather the fact that C's internal model doesn't comport well to important details about machines: * There's no distinction between registers and memory in C. A function parameter that is "register volatile _Atomic int" is completely legal and…

> * Missing operations such as popcount, cttz, simultaneous div/rem, or rotates.

gcc/clang offers builtins for most of these. And if you give the compiler the right march parameter it'll quite likely turn your hand-rolled code for these operations into the x64 instruction you're targeting. Try it on Compiler Explorer.

Post reply on HN