Hi Bruno, On Sun, Dec 14, 2025 at 01:20:29AM +0100, Bruno Haible wrote: > After reading [1] I wonder whether Gnulib should define, in some header file, > these macros: > > #define ARRAY_OF_EXACTLY(a,n) (*a)[n]
This is correct. That's the way to specify arrays of exactly n. However, as you say, it requires an indirection, which is hard to read and write. As you say, the indirection is a no-op, but it's quite unreadable. Most of the time, you should be fine with arrays of at least n. I'd ask you to please avoid arrays of exactly n in general. Use them sparingly, only if you really really need them. I'm working on something that will make these even less useful, BTW. > #define ARRAY_OF_AT_LEAST(a,n) a[static n] Please don't use [static n] ever. I want to remove it from the standard, and I think I'm quite advanced in convincing the right people. If you start using it, that could be negative to my efforts. The right way to specify an array of at least n is [n]. That's what GCC implements. That's what we intend to standardize. Please forget that [static n] ever existed. > > So that functions may be declared and defined as taking a parameter > TYPE ARRAY_OF_EXACTLY (PARAMETER, SIZE) > or > TYPE ARRAY_OF_AT_LEAST (PARAMETER, LOWER_BOUND_FOR_SIZE) > > This may be useful, because it enables gcc warnings (see attachment). This is not true. [static n] doesn't enable any GCC warnings, as far as I know. In [n] and [static n] have exactly the same diagnostics. I've compiled the example you attached, and see the same amount of diagnostics for both. [static n] enables some Clang diagnostic (which, AFAICS, is a strict subset of what GCC can diagnose), but honestly, Clang is really bad at diagnosing array bounds; rely on GCC, and forget about Clang. > On the other hand > - ARRAY_OF_EXACTLY requires writing an indirection (that is in fact a > no-op), > - ARRAY_OF_AT_LEAST is sometimes mis-used. > > Hmm. What do you think? TL;DR: - Please continue using [n]. - Try to avoid (*)[n]. - Definitely don't use [static n]. > > Bruno > > [1] https://lwn.net/Articles/1046840/ I think this post did a great disservice to C programmers. Have a lovely night! Alex -- <https://www.alejandro-colomar.es>
signature.asc
Description: PGP signature
