开发者

Pointer Array Question

开发者 https://www.devze.com 2023-01-31 14:50 出处:网络
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 dif

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

精彩评论

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