Why can't the size of a static array be made variable?

后端 未结 4 1153
猫巷女王i
猫巷女王i 2020-11-28 12:20

Related but not quite duplicate as it discusses C++:
can we give size of static array a variable

I am definin

4条回答
  •  清酒与你
    2020-11-28 12:46

    You cannot declare a static array of variable size because its space is allocated in the Data Segment (or bss segment in case of an uninitialized variable). Hence the compiler needs to know the size at the compile time and will complain if the size is not a constant.

    The underlying reason for this is the Data Segment size contributes to the size of the executable being generated, which obviously is created at compile time, and hence has to be fixed.

提交回复
热议问题