S
Sensei
Hi! I am still learning a lot reading thin NG, so I am now turning
here to clarify my doubts with VLAs. I hope I won't be that silly or
naive
In (6.7.5.2) I read about the correct declaration of arrays, although I
don't understand how a variable is handled by the compiler in declaring
a VLA. As I understand, an array must be declared with an integer size
specifier:
int x[3];
That would give a complete type, and I can use x happily. If no integer
is specified, then
int x[];
would be an incomplete type. This quite puzzles me, since I don't
understand how x[] could then be used in a code block. I can only think
about a function with two parameters, the incomplete array and the
size. Is it possible to make use of that variable in other ways?
The other doubt is about VLAs. If some variable is specified instead of
a constant, then the variable should be in the block. In the document I
see a prototype and a variable inside the code block:
void fvla(int m, int C[m][m])
....
int D[m];
My question is probably stupid, but how are C and D handled by the
compiler? My concerns are about some code like the following (mixed
code and variables, since it is C99):
/* checks about argc and argv */
int a = atoi(argv[1]);
/* follows some check about a */
int x[a];
I know it's risky, but it's allowed, isn't it? Is x allocated on the
fly as we enter the code block (somehow, on the heap or by any other
non C-related means) and then subsequently freed when exit? Then what
are the advantages (not counting the "syntactic sugar" I can think of)
of having VLAs into the standard?
I am puzzled, but forgive me: I am still learning a lot!
Thanks!
here to clarify my doubts with VLAs. I hope I won't be that silly or
naive
In (6.7.5.2) I read about the correct declaration of arrays, although I
don't understand how a variable is handled by the compiler in declaring
a VLA. As I understand, an array must be declared with an integer size
specifier:
int x[3];
That would give a complete type, and I can use x happily. If no integer
is specified, then
int x[];
would be an incomplete type. This quite puzzles me, since I don't
understand how x[] could then be used in a code block. I can only think
about a function with two parameters, the incomplete array and the
size. Is it possible to make use of that variable in other ways?
The other doubt is about VLAs. If some variable is specified instead of
a constant, then the variable should be in the block. In the document I
see a prototype and a variable inside the code block:
void fvla(int m, int C[m][m])
....
int D[m];
My question is probably stupid, but how are C and D handled by the
compiler? My concerns are about some code like the following (mixed
code and variables, since it is C99):
/* checks about argc and argv */
int a = atoi(argv[1]);
/* follows some check about a */
int x[a];
I know it's risky, but it's allowed, isn't it? Is x allocated on the
fly as we enter the code block (somehow, on the heap or by any other
non C-related means) and then subsequently freed when exit? Then what
are the advantages (not counting the "syntactic sugar" I can think of)
of having VLAs into the standard?
I am puzzled, but forgive me: I am still learning a lot!
Thanks!