5

Any idea why the 'stackalloc' keyword accepts a variable length?

If this instruction returns a pointer to a buffer allocated in the stack's frame, how the compiler manage that? It recompiles the function at runtime every time it's called to organize the stack frame?

Thanks.

andresantacruz
  • 1,676
  • 10
  • 17
  • Interesting, but I guess the JIT will just alloc on the fly given there is a minimum reserved stack size (IIRC). – leppie May 14 '16 at 20:38
  • Why would it need to recompile the function? A stack allocation only requires that the stack pointer be moved. – Michael Burr May 14 '16 at 20:39
  • @leppie hmm, and what about a buffer bigger than this reserved stack size? ;x – andresantacruz May 14 '16 at 20:39
  • @MichaelBurr isn't the stack frame in c# created at compile time? If so, the length of a variable buffer should alter the offset of other local variables, doesn't it? – andresantacruz May 14 '16 at 20:41
  • 5
    @ptr0x I would assume that the actual buffer is simply allocated *after* the original stackframe, so *after* the locals (just incrementing the end-of-frame counter), just storing the addr into the local; it would be easy enough to test that hypothesis, though, by comparing the value of the pointer the the address of the variable that holds the pointer – Marc Gravell May 14 '16 at 20:45
  • Thank you very much @MarcGravell! 'll test this hypothesis later but it make every sense. =] – andresantacruz May 14 '16 at 20:46
  • 1
    @ptr0x actually, in a test here, it looks like the stackalloc stuff *is before* the local, which I find confusing... still testing something – Marc Gravell May 14 '16 at 20:52
  • @MarcGravell hmm it can make sense if the compiler calculates the offset of each local variable based on the 'stackalloc' buffer size. The problem would rise if the buffer was allocated in the middle of the frame I guess. – andresantacruz May 14 '16 at 20:53
  • 2
    The stackalloc'ed stuff will be before the locals because the stack grows down. To alloc something on the stack all the runtime has to do is subtract the allocated amount from the stack pointer and give that address to the allocation (very high level overview - there are other details like making sure the stack remains aligned properly, etc, but that's fundamentally it). – Michael Burr May 14 '16 at 20:59
  • @MichaelBurr dammit, I just came back to add a "derp, the stack allocates downwards, so "after" in stack terms is earlier in raw memory, hence the result" - but you beat me to it! ;) – Marc Gravell May 14 '16 at 21:03
  • Very nice pointed @MichaelBurr. If you want to create an answer I'll accept. Thanks for all you guys! – andresantacruz May 14 '16 at 21:06

1 Answers1

6

Any idea why the stackalloc keyword accepts a variable length?

Because it is useful and desirable to be able to do so, and hence the language designers chose to allow it.

If this instruction returns a pointer to a buffer allocated in the stack's frame, how the compiler manage that? It recompiles the function at runtime every time it's called to organize the stack frame?

The localalloc instruction allocates the memory after the stack-current frame, and the assignment just stores the address back into your local. Hence: it is not necessary to recalculate anything, except to update the end of the stack-frame so that it doesn't get overwritten if we call into another method.

(note: as usual when discussing the stack, this is actually an implementation detail; the JIT is in theory at liberty to do allocate it from anywhere it wants as long as it respects the semantics)

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900