How does the C compiler know how to increase the size of an array?

My program has a line of code:

float cache[featureHeight-1];

where featureHeight is a function parameter. Now that the C compiler translates this to an assembly, how does it know how much space is allocated for the stack since featureHeight is not defined at compile time? Or does the compiler convert this to a malloc call backstage?

(C99 btw, no compiler errors or warnings, and the code works fine)

+4
source share
3 answers

malloc, , , , . . " " C99. , , / .

/, featureHeight-1 By sizeof(float) , . !

+4

.

( - ) , . , , , malloc. , , ( C, , ), , .

, alloca, , .

+2

C99 ( ).

This way your stack of the method stack will not be fixed, but the size will match your array.

+1
source

All Articles