4 ms·
In may ways you cant avoid more modern C because more modern standards make thing that were ambiguous more clear, and then compilers follow that even if you sel
by quelsolaar 5y ago
In may ways you cant avoid more modern C because more modern standards make thing that were ambiguous more clear, and then compilers follow that even if you select an older standard.
I think C99 introduces some bad things that should be keep out of any code base. Variable length arrays being the biggest one. Generics is another one. Being able declare variables anywhere doesn't make the code better but it makes styles diverge and that's a problem.
If Linux wants to adopt C99 they should define what parts of c99 should be adopted in their style guide.
- JoshTriplett 5y ago> Variable length arrays being the biggest one. The kernel already used VLAs as an extension, and is currently in the process of removing them from all the code using them.
- pjmlp 5y agoSomeone did a big mistake since 2018 then, https://www.phoronix.com/scan.php?page=news_item&px=Linux-Kills-The-VLA https://www.phoronix.com/scan.php?page=news_item&px=Linux-Ki...
- JoshTriplett 5y agoCorrection: I was mixing up VLAs (which were used before then, and removed) and zero-length/one-element arrays (https://www.kernel.org/doc/html/v5.10/process/deprecated.html#zero-length-and-one-element-arrays https://www.kernel.org/doc/html/v5.10/process/deprecated.htm...).
- nybble41 5y ago> Being able declare variables anywhere doesn't make the code better… It does, though. The ability to declare variables "anywhere" and allows a reduction in the portion of the code where a variable exists uninitialized, or initialized with a dummy value which should never be used, or holding an obsolete value which is not intended to be used. It also allows more variables to be declared `const` (which requires them to be initialized in the declaration, which must occur after the initial value is computed) which helps to detect accidental assignment and also makes the code easier to review. It's worth noting that you can more-or-less declare variables "anywhere" in C89 too—you just have to wrap the variable's scope in a compound statement. From that point of view, C99 doesn't change where variables can be declared. It just lets you remove some syntactic clutter.
- jstimpfle 5y agoI for one have read a lot of code with C89 declare-first style that was subjectively a lot less clear. This applies in particular to assign-once variables that depend on other calculations. stuff like this: [0]. I mean, what's the point of declaring variables like cp or err at the beginning when they're assigned first only halfway down? What is the point of having path around for the whole function, when it is a super temporary assign-once variable with a scope of 3 lines? I'm having a really hard time coming up with a justification for this, other than "this is the way we've always done it and we like it" (you can find actual rants in defense of this style from about 10 years ago). For-loop declarations are another instance of this that make the code more fluent IMO. And they help reducing the scope of variables to a strictly smaller block. I would find it hard to argue that this does not remove some bugs or improve readability. The only thing that declare-first has going for it is that the variables that are in use can be seen at once, a little bit like in a struct declaration. Looking at how optimizers butcher those variables depending on liveness makes this feature seem less valuable, though. [0] https://github.com/torvalds/linux/blob/2729cfdcfa1cc49bef5a90d046fa4a187fdfcc69/fs/ext4/file.c#L788 https://github.com/torvalds/linux/blob/2729cfdcfa1cc49bef5a9...
- pjmlp 5y agoThat is something I fully agree with.