Why is new int[n] valid when int array[n] is not?

前端 未结 9 2021
暗喜
暗喜 2020-12-31 02:00

For the following code:

foo(int n){
    int array[n];
}

I understand that this is invalid syntax and that it is invalid because the c++ sta

9条回答
  •  别那么骄傲
    2020-12-31 02:28

    In the first case you are allocating the memory space statically to hold the integers. This is done when the program is compiled and so the amount of storage is inflexible.

    In the latter case you are dynamically allocating a memory space to hold the integers. This is done when the program is run, and so the amount of storage required can be flexible.

    The second call is actually a function that talks to the operating system to go and find a place in memory to use. That same process does not happen in the first case.

提交回复
热议问题