I found some code today that confused me. It did something like this:
#include <stdio.h>
int main(int argc, char **argv) {
int x = 5;
int foo[x];
foo[0] = 33;
printf("%d\n", foo[0]);
return 0;
}
My Question is why does this work?
The array foo is on the stack so how could it be expanded by x?
I would have expected some thing like this:
#include <stdio.h>
int main(int argc, char **argv) {
int x = 5;
int foo[] = malloc(sizeof(int)*x);
foo[0] = 33;
printf("%d\n", foo[0]);
free(foo);
return 0;
}
Not that it is prettier or something but, I just wonder.
alloca. (there are differences in persistence, however)