by vortico · 7y ago · 0 comments
void foo(char *data, int len)
Perhaps cryptography functions with 256-bit outputs might be able to use this, but even when the array size is fixed, most crypto libraries (like OpenSSL) tend to have a size argument anyway, to encourage the caller to think about what they're doing, and for the library to check that 32 bytes are actually available, for example. void f(char (*n)[255]);
char array[6];
f(&array);
warns of "incompatible pointer types passing 'char (* )[6]' to parameter of type 'char (* )[255]'"This won't produce a diagnostic for f(NULL) like "static" does, but does have two properties that might be considered benefits:
1) The length is exact rather than a minimum.
2) The type of "* n" is still char[255], whereas a char[static 255] parameter is still a decayed pointer-to-char. Thus with the former sizeof(* n) behaves as expected inside of "f", yielding 255.
These are true of the array-in-struct method as well.
Thanks for commenting this.
With that, I wouldn't expect that any padding would be added since the array should already have the same alignment as the `struct` anyway - but compilers are known to do weird things from time to time, so it's definitely not impossible.
void wlr_matrix_multiply(float mat[static 9], const float a[static 9],
const float b[static 9]); void foo(const int n, const char data[n])
Is this also valid? void foo(const int n, const char data[static n])I've used this in code compiled with gcc and clang. Both accept this construct, but I'm not sure how effectively any compiler is able to use this information to emit warnings/errors.
void foo(char bar[static 1])
vs void foo(NOTNULL(char* bar))
Though I'm not sure whether this can be expressed as a macro, since you need to remove the "*"? #define NOTNULL
void foo(NOTNULL char *bar)
If you're using Objective-C, then nonnull is your friend.https://news.ycombinator.com/item?id=18787595
The compiler didn't complain when passed a pointer to NULL (ok, not the same as passing NULL.
void foo(char data[static 0], int len)
with 0 or 1 to signal at least zero/one elements are required (for example), and the pointer must be non-null.