0

This is the question:

How are variable length arrays implemented at machine code (or assembly code - I don't literally want to see some binary code) level? Since many local variables are stored in the .data segment, I guess something different must be done to support VLAs.

Here is an experiment I did which didn't help me find the answer:

I tried to obtain some clues as to the answer to this question by writing a very short code in C, and then disassembling the compiled output.

Here is that code:

  1 int main()                                                                      
  2 {                                                                               
  3                                                                                 
  4     int n = 255;                                                                
  5                                                                                 
  6     int array[n];                                                               
  7                                                                                 
  8     array[0] = 15;                                                                                                  
  9                                                                                 
 10 }

Compiled with gcc main.c. Disassembled with objdump a.out -M intel -d. Note, it might be the case that I could have used some better compiler options or options to the objdump command which would have made the output simpler, but I am not sure what these might be.

The output produced was significantly longer than I had initially expected, and looking through it I didn't find anything obvious which was able to answer the question.

FreelanceConsultant
  • 13,167
  • 27
  • 115
  • 225
  • 1
    Local (aka automatic) variables are never stored in the data segment. They are stored on the stack. I can write a more detailed answer shortly. – fuz Sep 12 '21 at 14:57
  • `gcc -O0` asm for VLAs [is really dumb](https://stackoverflow.com/questions/21182307/how-does-gcc-implement-variable-length-arrays#comment110533927_21182688), using actual `div` to round the allocation size to a multiple of 16 bytes IIRC. Look at a function that takes a function arg, and passes a pointer to the VLA to another function, so you can optimize without optimizing away the VLA. https://godbolt.org/z/ax7a18bzY / [How to remove "noise" from GCC/clang assembly output?](https://stackoverflow.com/q/38552116) – Peter Cordes Sep 12 '21 at 20:53
  • I have a half-finished answer to [How does GCC implement variable-length arrays?](https://stackoverflow.com/q/21182307) sitting around somewhere that I could maybe finish. Also related, though: [Understanding GCC's alloca() alignment and seemingly missed optimization](https://stackoverflow.com/q/52525744) – Peter Cordes Sep 12 '21 at 20:55

1 Answers1

0

VLA's are normally stored on the stack, as if you allocated them using alloca() or _alloca().

https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/alloca

rcgldr
  • 27,407
  • 3
  • 36
  • 61