Live data from Hacker News

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

news.ycombinator.com

861–870 of 978 posts

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

#861
post #524

Earlier quoted context omitted.

I'd say both statements are correct. Compiler implementers are happy when they don't have to care about some edge case because then the code is simpler. Thus, only for unsigned counters there is the extra logic to compile them correctly. That is my interpretation of "The opposite is the case". Writing a compiler is easier with lots of undefined behavior.

But that's backwards, the compiler writers are writing special cases to erase checks in the signed case. Doing the 'dumb' thing and mindlessly going through the written check is simpler which is why that's what compilers did for decades as de facto standard on x86.

The dump thing is a non optimizing compiler. GCC and LLVM contain many optimization phases. It is probably some normal optimization which is only "wrong" in the context of loop conditions.

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

#862

Earlier quoted context omitted.

>clean up the spec Would this involve further specification of bitfields? Feel implementation defined nature of bitfields limits potential

What parts of bitfields are implementation defined?

looking here https://en.cppreference.com/w/c/language/bit_field seems quite a bit. My main thought was how field's laid out in memory. Know would be big change with endianness but thought a standard check might be useful...?

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

#863
post #221

Earlier quoted context omitted.

It's a three year rotation: Python, C (Unix), C (Arduino). My goal with the class is to teach ideas that will stand the test of time. C (and Unix) certainly fit that bill. Happily, the tooling is the easiest part. Every student has a rasberry-pi running debian, no mouse, no window server, and no extraneous software. You can spool kids up on a nano-based C toolchain in one class period with remarkably few sharp edges.…

Neat! I think your choice of C and Python as the languages to be taught is very right. The idea of showing the use of C in both Desktop/Server and Embedded environments has long been the approach i had advocated. C is truly the de-facto "universal" language and students should be made aware of it from the start. I would suggest the following additions; * The Arduino "language" is C++. Use this as a gentle introductio…

C++ is the language with which I’m most comfortable, having used it professionally for the better part of a decade. I’ve gone back and forth on using it in HS. It’s abstractions are so opaque if you don’t already have a well developed model of programming.

For the arduino, I’m really interested in teaching control systems. So we will start with finite state machines and go from there to implementing PID controllers. I’m intrigued by the idea of avoiding the IDE, so will totally pick up that book!

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

#864

Earlier quoted context omitted.

Sure you can. int aFoo[]; has many legal array operations possible: *(aFoo+3) should work fine and return the 4th int in the array.

I think your star operator there is making the compiler cast your array to pointer implicitly.

Its all symbols, so you can say whatever. But if it looks like a pointer, walks like a pointer, and quacks like a pointer, Its A Pointer.

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

#865

Earlier quoted context omitted.

Is this a real concern, beyond 'experts panel' esoteric discussion? Do folks really put a number into an int, that is sometimes going to need to be exactly TYPE_MAX but no larger? I've gone a lifetime programming, and this kind of stuff never, ever matters one iota.

The very few times I've ever put in a check like that, I always do something like i < MAX_INT - 5 just to be sure, because I'm never confident that I intuitively understand off-by-one errors.

Same here. But I instead run a loop over a range around MAX_INT (or wherever the issue is) and print the result, so I know I'm doing what I think I'm doing. Exhaustive testing is quick, with a computer!

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

#866
Is there a way to append/extend a MACRO value ?

For example, I have a arbitrary number of includes, each of them declare a struct that need to be listed later on.

  #define MOD_LIST // start with an empty list

  #include "mod/a.c"
  // MOD_LIST is: a,

  #include "mod/b.c"
  // MOD_LIST is: a,b,
  
  Module modules[] = {
    MOD_LIST
  }

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

#867
post #605

Earlier quoted context omitted.

That's a really bizarre layout for your struct. Why don't you put the length first?

Why would it matter? The bytes aren't inline, this is just a struct with two word-sized fields. A possible tiny advantage for this layout is that a pointer to this struct can be used as a pointer to a pointer-to-bytes, without having to adjust it. Although i'm not sure that's not undefined behaviour.

I don't think that's undefined behavior. That's how C's limited form of polymorphism is utilized. For example, many data structures behind dynamic languages are implemented in this way. A concrete example would be Python's PyObject which share PyObject_HEAD.

https://github.com/python/cpython/blob/master/Include/object...

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

#868
post #457
post #327

Earlier quoted context omitted.

You do have to understand that compiler teams aren't saying something like "this triggers UB, quick just replace it with noop." It's just something that naturally happens when you need to reason about code. For example, consider a very simple statement. let array[10]; let i = some_function(); print(array[i]); The function might not even be known to the compiler at compilation time if it was from a DLL or something. B…

> But the compiler is like "hey! you used the result of this function as an index for this array! i must be in the range [0, 10)! I can use that information!" As a developer who has seen lots of developers (including himself) make really dumb mistakes, this seems like a very strange statement. Imagine if you hired a security guard to stand outside your house. One day, he sees you leave the house and forget to lock th…

I would be more concerned by the fact that if i is 10, then you already are in trouble ;)

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

#870

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…

I agree which brought me into looking at Zig. A future version of C might disallow macros, preprocessor, disallow circular libraries, include a module system, but allow importing legacy libs like Zig. Also something like llvm so we can automatically do static analysis, transforms would be great.

I am back to learning Zig for the way it addresses some pitfalls in C, but really because it is so easy for cross-platform work. Compiling a program to a Windows exe on my 2011 iMac Pro and then running it on my Windows machine was so easy and the error messages are so helpful when they do occur.

I am not an expert at either C or Zig, so I would appreciate any feedback from anyone who can more intelligently compare the two.

Post reply on HN