Scrolling randomly through the document my eyes almost popped when I saw
[Level 3, Chapter 16: Performance]:
A pointer to a collection of objects of unknown number. These functions
should use the VLA notation:
void func(size_t n, double a[n]);
I had no idea this was possible. Do compilers actually do anything with this
information or is it just informative?
EDIT: I didn't get a warning out of gcc
#include <stddef.h>
int last(size_t n, int a[n]) {
return a[n-1];
}
int main() {
int a[] = {0, 1, 2, 3, 4};
size_t siz = sizeof(a) / sizeof(a[0]);
return last(siz+1, a);
}
Compile:
$ cc --version
cc (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ cc -Wall -Wextra -std=c11 staticarr.c -o staticarr && ./staticarr ; echo $?
254
clang 3.8 warns about differing things:
$ clang-3.8 -Weverything -std=c11 staticarr.c -o staticarr && ./staticarr ; echo $?
staticarr.c:3:25: warning: variable length array used [-Wvla]
int last(size_t n, int a[n]) {
^
staticarr.c:3:5: warning: no previous prototype for function 'last' [-Wmissing-prototypes]
int last(size_t n, int a[n]) {
^
2 warnings generated.
Still, it's a neat idea from a documentation point of view, and GNU-like
compilers seem to accept it.
A bit before is more explanation of on the arr[static ?] notation. I was
aware of this and usually prefer to use (Clang/GCC) function annotations to
indicate NULL-ness of pointers and such, but I see the advantage in some
situations. I don't agree with everything in the document, but it contains a
lot of cool things.
I think it's worth clarifying that arrays declared in parameter lists are simply just pointers. It's 100% syntactic sugar. So the `int a[n]` is simply treated as `int * a`, and the value inside of the `[]` is ignored completely. This leads to the weird situation that you declared it as `int a[n]`, but `sizeof(a)` just gives the size of a pointer, not the size of the array.
The only special case is `int a[static n]`, which does tell the compiler that `a` has at-least `n` entries. But `a` is still just a pointer in this instance, so `sizeof(a)` still gives the size of a pointer.
Personally, I'd recommend avoiding the syntax purely because of the `sizeof` issue, but its up to you.
You're completely right, but I think it's worth clarifying that VLA notation in parameter lists, and actual VLAs are two completely different things. The example he showed doesn't actually use a VLA, it just looks like it does.
I don't see an obvious reason - that's all information that's available on compile-time, there should be no additional run-time cost - or what are you implying?
EDIT: I didn't get a warning out of gcc
Compile: clang 3.8 warns about differing things: Still, it's a neat idea from a documentation point of view, and GNU-like compilers seem to accept it.A bit before is more explanation of on the arr[static ?] notation. I was aware of this and usually prefer to use (Clang/GCC) function annotations to indicate NULL-ness of pointers and such, but I see the advantage in some situations. I don't agree with everything in the document, but it contains a lot of cool things.