4

I have a line of code in my program:

float cache[featureHeight-1];

where featureHeight is a function parameter. Now when the C compiler translates this to assembly, how does it know how much stack space to allocate, since featureHeight is undetermined at compile time? Or does the compiler convert this to a malloc call behind the scenes?

(C99 btw, no compiler errors or warnings, and code runs perfectly)

Michael Dorst
  • 8,210
  • 11
  • 44
  • 71
user1299784
  • 2,019
  • 18
  • 30
  • 1
    This was added in C99, but made optional in C11, so you need to (or should) check if a macro (I forget the exact name) is defined before using it, and use malloc and free instead if it is unavailable. This will allocated the memory on the stack until that particular function returns. (so don't do something like `return cache;`) – technosaurus Nov 18 '14 at 05:16
  • @technosaurus You're after `__STDC_NO_VLA__`. – Iwillnotexist Idonotexist Nov 18 '14 at 05:20
  • possible duplicate of [How does GCC implement variable-length arrays?](http://stackoverflow.com/questions/21182307/how-does-gcc-implement-variable-length-arrays) – phuclv Nov 18 '14 at 06:06
  • a related question http://stackoverflow.com/questions/714692/alloca-implementation – phuclv Nov 18 '14 at 06:18
  • http://stackoverflow.com/questions/19131712/declare-an-array-with-a-variable?lq=1 – phuclv Nov 18 '14 at 07:27

3 Answers3

4

Not a malloc call, usually, though I guess that would be possible. It simply reserves the necessary space on the stack and uses that. this feature is called a "variable length array" and was introduced in C99. Its semantics are identical to those of a normal compile-time array, except that they're sized/allocated at runtime.

As far as the lower-level/assembly language side of things goes, a multiplication of featureHeight-1 By sizeof(float) and a decrement of the stack pointer would be all that's required. Watch out for stack overflows!

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
  • 1
    The semantics differ slightly, e.g. the lifetime starts at the point of declaration (not at the beginning of the enclosing block) and a jump forward across a VLA declaration is not allowed (iirc). – mafso Nov 18 '14 at 11:44
3

There's no need to know the amount of space to reserve on the stack in advance.

On many architectures reserving space on the stack just involves subtracting a value (any value - no need to be static) from the stack pointer and then using that as a pointer to the value. So there is some runtime calculation going on, but nothing as complicated as a malloc. These are of course just implementation details (the C standard probably doesn't talk about stack pointers), but this is how it works in practice.

Some platforms even have a non-standard function such as alloca that does the same except via a function call.

Matti Virkkunen
  • 63,558
  • 9
  • 127
  • 159
2

C99 does have the ability to get dynamically sized automatic (stack-allocated) arrays.

So your method's stack frame will not be fixed, but sized in order to fit your array.

Thilo
  • 257,207
  • 101
  • 511
  • 656