Why does a C/C++ compiler need know the size of an array at compile time?

后端 未结 6 1000
旧巷少年郎
旧巷少年郎 2020-11-29 19:19

I know C standards preceding C99 (as well as C++) says that the size of an array on stack must be known at compile time. But why is that? The array on stack is allocated at

6条回答
  •  生来不讨喜
    2020-11-29 19:30

    It is not an extremely complicated thing to support, so the reason C89 doesn't allow this is not because it was impossible back then.

    There are however two important reasons why it is not in C89:

    1. The runtime code will get less efficient if the array size is not known at compile-time.
    2. Supporting this makes life harder for compiler writers.

    Historically, it has been very important that C compilers should be (relatively) easy to write. Also, it should be possible to make compilers simple and small enough to run on modest computer systems (by 80s standards). Another important feature of C is that the generated code should consistently be extremely efficient, without any surprises,

    I think it is unfortunate that these values no longer hold for C99.

提交回复
热议问题