codereviewanskquestions
codereviewanskquestions

Reputation: 14008

Array static allocation

int num = atoi(argv[1]);
unsigned long times[num];  

I have this code and I assumed it won't compile because I am trying to allocate the array using a value from a command line argument, which compiler doesn't know at the compile time. But I compiled this code and it worked. Can someone explain what is going on here?? Am I misunderstanding the basic concept of static allocation??

Upvotes: 2

Views: 149

Answers (1)

shengy
shengy

Reputation: 9749

C99 allows to allocate an array with a var. This is called variable length arrays aka VLA

I don't have the C99 in my hand, the section is 6.7.5.2 in C99, and the following links are from the internet.

vla - wikipedia

c99 - wikipedia

be aware that vla is not supported in c++, more information here

Upvotes: 5

Related Questions