As you may know, VLA's haves pros and cons and they are optional in C11.
I suppose that the main reason to make VLA's optional is: "the stack can blow up":
int arr[n]; /* where n = 1024 * 1024 * 1024 */
but what about pointer to VLA's?
int m, n;
scanf("%d %d", &m, &n);
int (*ptr)[n] = malloc(sizeof(int [m][n]));
In this case, there is no risk to blow up the stack, and IMO they are extremely useful.
My question is:
Could the committee have preserved pointers to VLA's, making the VLA's to non-pointer types optional?
Or one thing implies the other?
(Excuse my poor english)
int (*ptr)[n]
is just a pointer to an array of sizen
. Then
here can be completely ignored by the compiler as it has no purpose.int *ptr;
is exactly the same. – Allseedn
affects how the pointer arithmetic is done onptr
. It most certainly cannot be ignored. – Welchptr + x
must be equal to(char*)ptr + x * (sizeof(int[n]))
- usual pointer arithmetic in arrays. Same as if the row size was a constant expression. In this case it just isn't. – Welchptr + 1
=ptr + 4 bytes
if you declareint *ptr;
,ptr + 1
=ptr + (4 bytes * n)
using a pointer to VLA – Vespidptr + i
, being equal toptr[i]
and addresses thei
th row of the array. How to address an individual int? (My apologies to distract from Keine Lust's original question). – Allseedptr[i][j]
will address an individual int. – Welch