[HN Gopher] Checked-size array parameters in C
___________________________________________________________________
Checked-size array parameters in C
Author : chmaynard
Score : 16 points
Date : 2025-12-03 20:37 UTC (2 hours ago)
(HTM) web link (lwn.net)
(TXT) w3m dump (lwn.net)
| aaaashley wrote:
| Funny thing about that n[static M] array checking syntax-it was
| even considered bad in 1999, when it was included:
|
| "There was a unanimous vote that the feature is ugly, and a good
| consensus that its incorporation into the standard at the 11th
| hour was an unfortunate decision." - Raymond Mak (Canada C
| Working Group), https://www.open-
| std.org/jtc1/sc22/wg14/www/docs/dr_205.htm
| jacquesm wrote:
| It wasn't considered bad, it was considered ugly and in the
| context given that is a major difference. The proposed
| alternative in that post to me is even more ugly so I would
| have agreed with the option that received the most support, to
| leave it as it was.
| o11c wrote:
| Better option: just wrap it in a unique struct.
|
| There are perhaps only 3 numbers: 0, 1, and lots. A fair argument
| might be made that 2 also exists, but for anything higher, you
| need to think about your abstraction.
| Veserv wrote:
| Pointer to array is not only type-safe, it is also objectively
| correct and should have always been the syntax used when passing
| in the address of a known, fixed size array. This is all a
| artifact of C automatically decaying arrays to pointers in
| argument lists when a array argument should have always meant
| passing a array by value; then this syntax would have been the
| only way to pass in the address of a array and we would not have
| these warts. Automatic decaying is truly one of the worst actual
| design mistakes of the language (i.e. a error even when it was
| designed, not the failure to adopt new innovations).
| jacquesm wrote:
| Fully agreed, and something that is hard to fix. This guy is
| trying really hard and with some success:
|
| https://news.ycombinator.com/item?id=45735877
| nikeee wrote:
| GCC also has an extension to support references to other
| parameters of the function: #include <stddef.h>
| void foo(size_t n, int b[static n]);
|
| https://godbolt.org/z/c4o7hGaG1
|
| It is not limited to compile-time constants. Doesn't work in
| clang, sadly.
| Arch-TK wrote:
| [delayed]
___________________________________________________________________
(page generated 2025-12-03 23:00 UTC)