-
Notifications
You must be signed in to change notification settings - Fork 14.7k
Description
GCC ticket: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=121331
Cc: @AaronBallman
This is a stronger version of -farray-parameters-are-const, proposed in
#151526.
It would make array parameters work as real arrays in every way: sizeof(), typeof(), _Generic(), ...
This would affect the function scope, but not it's callers.
Let's write some examples, to make it clear:
void
f(int size, char buf[size], pid_t pid)
{
if (stprintf(buf, _Countof(buf), "/proc/%d/", pid) == -1)
return;
...
return;
}
char *a = malloc(100);
f(100, a, 0); // Ok; at call site pointers are valid.
void
g(int size, char buf[size], pid_t pid)
{
buf = NULL; // error: assignment to expression with array type
}
void
h(char *buf;
int size, char buf[size], pid_t pid); // error: redeclaration with different type
void
i(char buf[];
int size, char buf[size], pid_t pid); // okay
void
i(char buf[size];
int size, char buf[size], pid_t pid); // okay
This is a breaking change, so it must be opt-in.
However, there shouldn't be much code that is relying on the old behavior, as most of it is already diagnosed by default.
With this flag enabled, the only difference with real arrays would be that these could be null. Other than that, they'd be real arrays.
(And also, of course, that one could use _Nullable
within []
, to mark the array as possibly null.)