Is there any overhead for using variable-length arrays?

前端 未结 4 1243
北荒
北荒 2020-11-28 06:22

Is there some overhead of using variable-length arrays? Could the size of array be passed via command line argument at run time? Why is it introduced, compared to automatic

4条回答
  •  时光说笑
    2020-11-28 07:17

    I just wonder if there is some overhead of using variable-length arrays?

    Nope

    Can the size of array could be passed via command line argument at run time?

    Yes.

    Why is it introduced, compared to automatic and dynamically allocating an array?

    Automatic allocated only allows a fixed size known at compile time.

    Dynamically allocating (malloc) will store the array on the heap, which has a large memory space, but is slower to access.

    VLA works by placing the array in the stack. This makes allocation and access extremely fast, but the stack is usually small (of a few KB), and when the VLA overflowed the stack, it's indistinguishable from an infinite recursion.

提交回复
热议问题