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.
精彩评论