开发者

Pointer Array Question

Creating an array can be done both of these ways? But isn't all an array is the beg开发者_高级运维inning of the array address and allocate enough bytes for the type. So my question is what's the difference either using a pointer to an array of bytes or using just the first option?

int numbers[10];

int* num = new int[10];
or
int* num = new int(10);


Your second version there declares a pointer to an integer initialised to 10. That's not an array.


int array[10]; has two different behaviors. Used inside a function, it will allocate 10 uninitialized ints on the stack. Outside a function, it will allocate 10 zero-initialized ints in BSS memory.

new int[10]; allocates ten uninitialized ints on the heap.

new int(10); allocates a single int on the heap, with value 10.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜