https://gcc.gnu.org/bugzilla/show_bug.cgi?id=121271
Bug ID: 121271
Summary: New dialect flag: -fconst-array-parameters
Product: gcc
Version: 16.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: [email protected]
Target Milestone: ---
This would change the dialect to make array parameters implicitly const.
The following code:
int
foo(int size, char buf[100], pid_t pid)
{
if (stprintf(buf, _Countof(buf), "/proc/%d/", pid) == -1)
return -1;
...
return 0;
}
would be equivalent to
int
foo(int size, char buf[const 100], pid_t pid)
{
if (stprintf(buf, _Countof(buf), "/proc/%d/", pid) == -1)
return -1;
...
return 0;
}
This would allow one to safely use _Countof() with array parameters, without
the pointer being advanced accidentally, which would turn the size information
outdated.
We can't make this the default behavior, as it would break existing code, but
programmers might want to change the behavior in their own programs (I do).
That would make it so that we don't need to write const in every array
parameter to be able to use _Countof() on them. (I've written a proposal for
extending _Countof() to work on array parameters, and a constraint would be
that they need to be const-qualified.)