Variable Length Array

拥有回忆 提交于 2019-12-05 20:58:12

问题


I would like to know how a variable length array is managed (what extra variables or data structures are kept on the stack in order to have variable length arrays).

Thanks a lot.


回答1:


It's just a dynamically sized array (implementation-dependent, but most commonly on the stack). It's pretty much like alloca in the old days, with the exception that sizeof will return the actual size of the array, which implies that the size of the array must also be stored somewhere (implementation-dependent as well, but probably on the stack too).




回答2:


The size of variable length arrays is determined on run-time, instead of compilation time.
The way it's managed depends on the compiler.
GCC, for instance, allocates memory on the stack.
But there is no special structure. It's just a normal array, whose size is known at run-time.




回答3:


alternatively you could use some containers, e.g. ArrayList in java or vector in c/c++



来源:https://stackoverflow.com/questions/2759294/variable-length-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!