Declaring array of int

前端 未结 8 499
-上瘾入骨i
-上瘾入骨i 2020-12-07 13:11

Is there any difference between these two declarations?

int x[10];

vs.

int* x = new int[10];

I suppose th

8条回答
  •  执念已碎
    2020-12-07 13:46

    First case: x is created on stack/data segment based on whether it's non-static local variable or static/global variable. And address of x is not modifiable.

    Second case: 'x' is a pointer pointing to an array created generally on heap (free store). You can change x pointing to something else also. Moreover, you need to take care of deallocating it by using delete[] x;

提交回复
热议问题