tstanisl
Re:C99 doesn't need function bodies, or 'VLAs are Turing complete'
on Friday August 05, 2022 @06:23AM (#32354077)
int A[100000000];
Also has no protection.
Re:C99 doesn't need function bodies, or 'VLAs are Turing complete'
on Friday August 05, 2022 @06:19AM (#32354049)
There are multiple false myths about VLAs.
Please known that VLA is about the typing not about the storage.
The line:
typedef int T[n];
is the essence of VLA-ness, not `int A[n]`. The array of VLA type can have any kind of storage one wants.It can be stack
T a;
It can be heap T *a = malloc(sizeof *a);
It can be even infamous alloca() T *a = alloca(sizeof *a);
Please stop talking about this "VLA is stack-base vector" crap because it means that one does not understand what VLAs are about. I admin that automatic VLAs are pretty much always wrong but this use case is a tiny bit of the realy functionality of VLA types.VLA were added to language to handle multidimensional arrays.
And the really shine at this task. Even C++ has not good alternative for it. Vector of vectors is a really crappy data structure.
A few examples when working with square matrices:
- allocation on stack
float A[n][n];
- allocation on heap float (*A)[n] = malloc(sizeof(int[n][n]));
...
free(A);
- indexing A[i][j]
- passing to functions: void add(int n, float A[static n][n], float B[static n][n], float RES[restrict static n][n]);
...
Please show me something as simple, effective, self-documenting and elegant in C++.