Is the size of an array determined at compile time?

后端 未结 3 1905
长情又很酷
长情又很酷 2021-01-19 19:25

When I was reading about the array initialization in this tutorial. I found out this note.

type name [elements];

NOTE: The e

3条回答
  •  失恋的感觉
    2021-01-19 20:12

    Please check if the following answers help in giving you clarity about this.

    Static array vs. dynamic array in C++

    Static arrays are created on the stack, and necessarily have a fixed size (the size of the stack needs to be known going into a function): int foo[10];

    Dynamic arrays are created on the heap. They can have any size, but you need to allocate and free them yourself since they're not part of the stack frame: int* foo = new int[10]; delete[] foo;

    You don't need to deal with the memory management of a static array, but they get destroyed when the function they're in ends

    Array size at run time without dynamic allocation is allowed?

    C99 standard (http://en.wikipedia.org/wiki/C99) supports variable sized arrays on the stack. Some of the compilers might implement these standards and support variable sized arrays.

提交回复
热议问题