I am mindblown by this small code:
#include <stdio.h>
int main()
{
int limit = 0;
scanf("%d", &limit);
int y[limit];
for (int i = 0; i<limit; i++ ) {
y[i] = i;
}
for (int i = 0; i < limit; i++) {
printf("%d ", y[i]);
}
return 0;
}
How on earth this program is not segment-faulting as limit (size of the array) is assigned at runtime only?
Anything recently changed in C? This code shouldn't work in my understanding.
limithas at the point of definition -- hopefully that isn't negative, very large, or undefined due to assumptions about user input.scanfsucceeds). The only problem is whenlimitis very large (>10000 or so depending on your platform), the program might crash because of a stack overflow as variable length arrays (VLAs) are usually stored on the stack. Another thing that can happen is that your code cannot by compiled if the compiler doesn't implement VLAs.