Live data from Hacker News

Tell HN: C Experts Panel – Ask us anything about C

news.ycombinator.com

251–260 of 978 posts

Re: Tell HN: C Experts Panel – Ask us anything about C

#251

Earlier quoted context omitted.

As a member of the development team for a C static analyzer, I use OCaml, which is also my favorite programming language, but that is because I'm from the generation in which it was the new thing (I learnt it when it had the same level of maturity as Rust, at a time when Rust didn't exist). It helps that it's perfect for writing compilers and static analyzers. There are a lot of problems that seem a good match for Ru…

Why won't you ever find time? It should only take a good 20 hours of reading and playing with code before you start to grok it.

I spent the early part of my career bragging about how many programming languages I knew, and the later part of my career complaining about how I don't know any of them well enough.

Re: Tell HN: C Experts Panel – Ask us anything about C

#252

As there are a lot of C-masters lurking in this thread: How can one process unicode (UTF-8) properly in C? As a CJK person, I wish there was a robust solution. Are there any standardized ways or proposals? (Using wchar doesn't count.)

Check this: http://utf8everywhere.org/

Basically store the text as char arrays, and convert them when needed. Meanwhile, you could use this single file header: https://github.com/RandyGaul/cute_headers/blob/master/cute_u...

Re: Tell HN: C Experts Panel – Ask us anything about C

#253
post #163

Now that C2x plans to make two's complement the only sign representation, is there any reason why signed overflow has to continue being undefined behavior? On a slightly more personal note: What are some undefined behaviors that you would like to turn into defined behavior, but can't change for whatever reasons that be?

Another approach would be a standard library of arithmetic routines that signal overflow. If people used them while parsing binary inputs that would prevent a lot of security bugs. The fact that this question exists and is full of wrong answers suggests a language solution is needed: https://stackoverflow.com/questions/1815367/catch-and-comput...

You can enable this in GCC on a compilation unit basis with `-fsanitize=signed-integer-overflow`. In combination with `-fsanitize-undefined-trap-on-error`, the checks are quite cheap (on x86, usually just a `jo` to a `ud2` instruction).

(Note that while `-ftrapv` would seem equivalent, I've found it to be less reliable, particularly with compile-time checking.)

Re: Tell HN: C Experts Panel – Ask us anything about C

#254

char effectively behaves as a signed type, making it unsuitable for binary operations (e.g. UTF-8 manipulation). I/O functions deal with char pointers, so using unsigned type like uint8_t requires casting back and forth. Is there any way out of this problem, and am I already breaking the aliasing rules with that cast?

There are no aliasing differences between uint8_t and char as far as I know.

Re: Tell HN: C Experts Panel – Ask us anything about C

#255
post #69

Earlier quoted context omitted.

FWIW, Annex K is not currently deprecated.

It's not commonly available though, e.g. on Linux/BSD systems...

Correct -- it would be nice if the glibc maintainers would reconsider their opinion of supporting the optional Annex K functionality. There is definitely user demand for the feature.

Re: Tell HN: C Experts Panel – Ask us anything about C

#256

Are there any plans to "clean up C"? A lot of effort has been put into alternative languages, which are great, but there is still a lot of momentum with C, and it seems that a lot of improvements that could be done in a backwards compatible way and without introducing much in the way of complexity. For example: - Locking down some categories of "undefined behaviour" to be "implementation defined" instead. - Proper ar…

What does clean up c mean?

Re: Tell HN: C Experts Panel – Ask us anything about C

#257
post #162

Earlier quoted context omitted.

So in a corner case where you have a loop that iterates over all integer values (when does this ever happen?) you can optimize your loop. As a consequence, signed integer arithmetic is very difficult to write while avoiding UB, even for skilled practitioners. Do you think that's a useful trade-off, and do you think anything can be done for those of us who think it's not?

No, it's exactly the opposite. Without UB the compiler must assume that the corner case may arise at any time. Knowing it is UB we can assert `n+1 > n`, which without UB would be true for all `n` except INT_MAX. Standardising wrap-on-overflow would mean you can now handle that corner case safely, at the cost of missed optimisations on everything else.

I hadn’t understood the utility of undefined behaviour until reading this, thank you.

Re: Tell HN: C Experts Panel – Ask us anything about C

#258
1. How likely are named constants of any types to be included in C2x? I'm referring to the idea of making register const values be usable in constant expressions.

2. Is there, or was there ever a proposal to make struct types without a tag be structurally typed? This would not break backwards compatibility as far as I can see, and would make these types much more useful as ad-hoc bags of data. Small example:

  struct {size_t size; void *data;} data = get_data();
  int hash = hash_data(data);
I believe there was at least one proposal about error handling that more or less relied on the above to be valid semantically.

3. Is there any interest in making the variadic function interface a bit nicer to use? I would like to bring back an old feature and have an intrinsic to extract a pointer from the variadic parameter list, so that we can iterate over it ourselves (or even index directly).

  void *arg_ptr = va_ptr(last);
More out there would be a parameter that would be implicitly passed to a variadic function to indicate the number of arguments.

  void variadic(..., va_size count) {
  
  }

  variadic(10, 20, 30); // count would be three

Re: Tell HN: C Experts Panel – Ask us anything about C

#259

Earlier quoted context omitted.

Microsoft in particular has a simple approach to this with things like DWordMult(). if (FAILED(DWordMult(a, b, &product))) { // handle error }

Clang and GCC's approach for these operations is even nicer FWIW (__builtin_[add/sub/mul]_overflow(a, b, &c)), which allow arbitrary heterogenous integer types for a, b, and c and do the right thing. I know there's recently been some movement towards standardizing something in this direction, but I don't know what the status of that work is. Probably one of the folks doing the AUA can update.

We've been discussing a paper on this (http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2466.pdf) at recent meetings and it's been fairly well-received each time, but not adopted for C2x as of yet.
Post reply on HN