C++ - dynamic array and difference between [] and ()

Hello,

What are the differences between [] and () when we want to create a dynamic array?

It seems ptr1 assigns first element 20.

1
2
3
	int* ptr = new int(20);
	int* ptr2 = new int[20];
yes. 1 is ONE integer with a VALUE of 20.
2 is 20 integers in an 'array' / memory block format, with unknown value.

so
int *p3 = new int[100](); is 100 integers all set to 0, but for some reason you can't do a block set this way, it only supports zero initialize if I understand it correctly (I don't use this syntax).
Last edited on
L1 ptr is allocated memory for 1 int initialised to 20

L2 ptr2 is allocated memory for 20 ints that are uninitialized.


ptr memory is freed using

 
delete ptr;


ptr2 memory is freed using

 
delete [] ptr2;



Topic archived. No new replies allowed.