c++ - Array size at run time without dynamic allocation is allowed? -
i've been using c++ few years, , today saw code, how can legal?
int main(int argc, char **argv) { size_t size; cin >> size; int array[size]; for(size_t = 0; < size; i++) { array[i] = i; cout << << endl; } return 0; }
compiled under gcc.
how can size determined @ run-time without new
or malloc
?
just double check, i've googled , similar codes mine claimed give storage size error.
even deitel's c++ how program p. 261 states under common programming error 4.5:
only constants can used declare size of automatic , static arrays.
enlight me.
this valid in c99.
c99 standard supports variable sized arrays on stack. compiler has chosen support construct too.
note different malloc
, new
. gcc
allocates array on stack, int array[100]
adjusting stack pointer. no heap allocation done. it's pretty _alloca
.
Comments
Post a Comment