4 ms·
That's kind of his point though - don't rely on your intuition for how most common compilers work! Learn enough to write correct code!
by advisedwang 3y ago
That's kind of his point though - don't rely on your intuition for how most common compilers work! Learn enough to write correct code!
- akira2501 3y agoWriting C in the blind for a quiz and deploying tested C to known platforms are two entirely different things.
- dwattttt 3y agoNot as different as we'd like; "we deployed our C to the platforms we need" works until the minute you change compiler/version/flags, and find out you were relying on something not guaranteed at all. My personal gotcha was finding out I was relying on shifting an unsigned integer by the bit size of the integer to be 0, as if "you shifted every bit out of the integer, leaving 0". Shift any size under the integer's size in bits? Yep, those bits were shifted out leaving 0. Shift that last bit? Nope, that's Undefined Behaviour, and suddenly it's not 0 just because you changed a flag.
- jovial_cavalier 3y agoNo one has written a C program of any complexity with zero undefined behavior. Noting its existence as if it's some kind of revelation is trite and annoying.
- mtlmtlmtlmtl 3y agoYeah there's a whole genre of talks, blog posts, online rants of the form: "So you think you know C, huh? What does this horrible piece of code do that no one would ever write and if you see it should be nuked from orbit? <ridiculous contrived example>" I just clicked I don't know on all of em, cause I realised what the metagame was from a mile away. Some of these things are at least a little insightful, but overall, kind of a waste of time past a certain point, unless you work on a compiler or something like that. Much more constructive would be making resources on best practices for things like memory management, string handling, knowing where the footguns are in the stdlib and other common libs, managing complexity etc. Most bad C code isn't because of some standards gotcha, it's because of those things.
- n_plus_1_acc 3y agoExcept there is some very crazy code out there, and if you come across it and took at least one such quiz, you know you'll have to be careful.
- mtlmtlmtlmtl 3y agoOh yeah, I've seen some shit, having done a fair bit of professional C work, mostly in the embed space. But if I'm in an existing codebase, and I see a certain volume of this kind of "potential UB everywhere" code, my first instinct isn't let's spend however long trying to understand what exactly this code may or may not be doing/relying on according to dusty corners of the standard and the datasheet(sometimes you may have to, but I find it's rare). I prefer to approach it as "what is this code supposed to do, and how can it be done more sanely? Usually I find I can replace the crazy code with sane code in a fraction of the time it would take to fully understand the crazy code.
- pardoned_turkey 3y agoThe author wasn't asking you to write code, but to analyze some existing snippets. And it wasn't about "intuition about how compilers work", because not all of these deal with undefined behavior. Some are merely unspecified - i.e., platform-dependent in a non-crazy way (example: big endian vs little endian). So the gotcha is that you made the reader assume you might be talking about a modern Intel or ARM CPU, but what you really meant is that the return value will be different on PDP-11. Frankly, seems pedantic.
- Affric 3y agoIt’s all pedantry until you are writing software for a 1970s nuclear power plant. /s
- chasil 3y agoTake the manipulation of the space, for example. Most of us would say that it has a value of 32 as defined by ASCII. This would be wrong on a platform that was using EBCDIC. We all have to make assumptions in our programs, which are sometimes wrong.
- sweetjuly 3y agoHuh, I didn't consider than angle. I said "I don't know" based on the fact that it could exhibit UB if the char container is signed and small enough that it cannot hold 32 * 13. Good to know though that you can't rely on the platform using ASCII!
- mark-r 3y agoSerious question, is there a C compiler that uses EBCDIC?
- chasil 3y agoDefinitely exists for IBM zOS. RedHat Linux has also been ported to this platform; it's probably ASCII, but not sure. Oracle has a native client on zOS that does transparent conversion between ASCII and EBCDIC. It was probably built with such a compiler. There was a paper that the original port of Research UNIX ran as a client on an IBM TSS/370 kernel, which likely was not ASCII. https://gunkies.org/wiki/UNIX/370 https://gunkies.org/wiki/UNIX/370