Live data from Hacker News

A critique of "How to C in 2016"

github.com

81–90 of 181 posts

Re: A critique of "How to C in 2016"

#81

Earlier quoted context omitted.

What? This comment is directly related to whether advice "don't use C if you can avoid it" is good or not.

And arguing that point is just the language war, nothing more, nothing less. I'd get as much mileage telling people that they shouldn't use Emacs unless they have to.

I think you're wrong. It's valid to discuss the pros and cons of languages, and it's valid to discuss whether a language is a good tool for any job.

But your thoughts are still valid and contribute to the conversation.

Re: A critique of "How to C in 2016"

#82
post #46
post #37

Earlier quoted context omitted.

I fail to see how C is more portable than any other memory safe systems programming language, other than having the fortune that for 30 years OS and hardware vendors decided to only support C compilers.

Compilers. Compilers for everything. You can hype your favorite systems language all you want, but if there is no compiler, it wont compile. >OS and hardware vendors decided to only support C compilers. If you want your favorite language X to work everywhere C does. Just make X-to-C compiler.

Of course, but that is an issue of implementation availability not language quality.

I remember the days when using C meant getting an account to the university (or work) UNIX system.

Re: A critique of "How to C in 2016"

#83
post #46

Earlier quoted context omitted.

Compilers. Compilers for everything. You can hype your favorite systems language all you want, but if there is no compiler, it wont compile. >OS and hardware vendors decided to only support C compilers. If you want your favorite language X to work everywhere C does. Just make X-to-C compiler.

X-to-C compiler isn't as nice as it could be, because you're writing code for some weird chip and need to twiddle some bits for IO using a macro defined in a header somewhere, and that doesn't play nicely with your pet X-to-C compiler. Not an ideal situation, for sure, but a real one.

It's possible for translator understand headers and macros and generate bindings for them. Even for function macros. For example, GNAT has macro binding although it's not complete.

Btw. There is nothing "pet" in X to C translators. You pay good money for a good Ada to C compiler.

Re: A critique of "How to C in 2016"

#84

Earlier quoted context omitted.

calloc is not relevant to that issue - it's an out-of-bounds read. (Good ASLR does help, by turning such issues into crash bugs.)

Heartbleed wasn't an out-of-bounds read.

If OpenSSL had used calloc() everywhere instead of malloc(), Heartbleed would have still happened.

Re: A critique of "How to C in 2016"

#85
post #5

> Zeroing memory often means that buggy code will have consistent behavior; by definition it will not have correct behavior. And consistently incorrect behavior can be more difficult to track down. I agree that using calloc should not be an excuse for writing sloppy initialization code. But in my experience, inconsistently incorrect behaviour (e.g. heisenbugs that tend to appear and disappear depending on the content…

I mean, reading zeroed out memory is not really incorrect behaviour anyway. If you know that it was zeroed out, then you're really just reading memory that was initialised with a specific default value.

That being said, zeroing out everything you allocate will make it difficult for the kernel's paging system to defer allocating you a physical page until you actually need it, so you will need to be a lot more careful with your allocations if your application uses relatively large amounts of memory and you need to work with memory constraints.

Re: A critique of "How to C in 2016"

#86

Earlier quoted context omitted.

I cannot tell if you are in favour of, or against, the principle that one should pick the right tool for the job.

Saying "just pick the right tool" is a worthless statement when the question is the difficulty of doing that. Would you enter a discussion about curing cancer or depression and announce "just pick the right cure for the disease" and then get upset when people don't agree with you?

Now I can understand your argument. I upvoted you because I like to encourage clarity. This sounds patronising but that's the limitations of a text interface for you.

I think you're arguing that sometimes it's difficult to work out what the right tool is for the job. That's true, and not what I'm talking about. I'm saying that the principle of selecting the right tool for the job is sound, and that sometimes C is the right tool for the job and should be selected.

Re: A critique of "How to C in 2016"

#87
post #56

I find myself to disagreement with almost ALL the points this "critique" makes. They are either minor pedantic corrections, "it's how it's always been done" things, or "you might have your reasons for doing it in a bizarro way" affairs. Sorry, but for 90% of use cases, the original article has better advice.

Agreed. Most of the integer stuff is pedantry ("doesn't make it non-standard" -- who cares, the point being made was that these types are better). There are some valid and useful points being made, but most would serve better as caveats on the original article rather than straight out "don't listen to this".

Re: A critique of "How to C in 2016"

#88
post #9

I disagree with many of the arguments. Many of them are ignoring the reality of today. One example: > If you want bytes, use unsigned char. If you want octets, use uint8_t Bytes and octets are the same today. I never came across a system where this wasn't true. I was never told about a system where this wasn't true. I wasn't even told that there once were systems where this wasn't true until fairly recently.

Hi, I'm working on one. It's a recent DSP with pretty massive processing power that's powering some really, really expensive machines (six figures a piece). A byte is 32 bits here. This is common on a lot of VLIW machines.

I guess uint8_t et co. appeared out of the plight of many systems programmer who needed to do things like "hand these precisely 8 bits to this peripheral". On this machine, getting only these specific 8 bits out of a char requires bit twiddling trickery (it doesn't support unaligned memory access), but if the peripheral you're talking to (e.g. a temperature sensor) calls 8 bits a byte, you're going to have to send it 8 bits, not teach it that a byte doesn't always mean 8 bits.

Prior to stdint.h (and on any platform without a C99-enable compiler, which are neither few nor unused), we used to work around this crap with a bunch of custom macros defined in hardware-specific header files. It was a mess and you needed to learn a lot more about every specific platform's compiler than you ever cared about. And it was particularly beautiful on bi-endian platforms, oh yes, those were so beautiful.

C99 came and said you know what, this is so platform-and-compiler-specific that the compiler should worry about it and that accounted for a massive relief of about 20% of my daily stress dose.

You should use it when you know that you need something to be exactly 8 bits long. Which may or may not be a byte (that's why it frickin' says int8_t and uint8_t, not byte and ubyte).

Edit: I'm going to piggyback on my comment and gently ask anyone to consider not blindly following the advice to inline the declaration of b in code like this:

    void test(uint8_t input) {
        uint32_t b;

        if (input > 3) {
            return;
        }

        b = input;
    }
In many (most?) cases, that's good advice, but declaring it (and any other local variables) in the beginning has the major advantage of allowing me to read the first line of the functions and tell how much space is being allocated on the stack when the function is called.

This is not only nice to know for performance reasons, it's also useful information for platforms that have limited stack size or -- my favourite! -- platforms that lack a MMU, so when your stack runs into useful data, your system doesn't crash, it just starts acting funny.

Re: A critique of "How to C in 2016"

#89

Earlier quoted context omitted.

Those compilers exist because of path dependency from unix not from any unique qualities of C

This makes no sense and I believe you are incorrect. When Analog Devices churned out the TigerSHARC processor, they did not decide to write a C compiler for it because of unix path dependencies.

C become widespread because of UNIX, just like JavaScript got widespread because of the browsers ubiquity.

Eventually developers wanted to take home to their 8 bit computers what they were coding at the university and work.

So RatC, Small-C and many other C dialects were born.

Just like node.js nowadays, with UNIX adoption becoming the basis of the 80's graphical workstations, the pressure to have easy access to C compilers increased.

Also back in the day, one paid for compilers, so the majority went to whatever what the OS official system programming language.

So this is how we ended up in this situation.

Re: A critique of "How to C in 2016"

#90
post #72
post #52

> Converting to uintprt_t will give you some result -- but that result won't be useful. Unless you're writing low-level system code, don't do any pointer comparison or arithmetic unless both pointers point into or just past the end of the same object. I disagree. There are situations where you need an ordering between unrelated objects, but the precise order doesn't matter, only that it's consistent. An example is wh…

I think comparing (using <) pointers to unrelated objects is undefined behavior. So you do not get a consistent ordering.

The part I quoted is talking about casting the pointers to uintptr_t (which is defined behavior) and comparing the resulting uintptr_t (since it's an integer, comparing it is defined behavior). It's not talking about comparing the pointers directly (which is undefined behavior). His argument is that, while defined behavior, "(uintptr_t)ptr_a < (uintptr_t)ptr_b" is useless; I disagree, since it's a well-known trick to prevent AB/BA type deadlocks.
Post reply on HN