When allocating memory for a pointer int* myPointer
, what is this statement doing?
myPointer = new int(8);
Versus this statement?
myPointer = new int[8];
I'm programming in C , and I know that using new
will allocate memory on the heap, to which myPointer
will point. But why does one statement use parenthesis instead of brackets? I know that brackets in myPointer = new int[8];
are used for an array, but what is the myPointer = new int(8);
doing exactly?
CodePudding user response:
myPointer = new int[8];
allocates an array of 8 int
s, and assigns the address of the beginning of the array to myPointer
.
myPointer = new int(8);
allocates one int
, initializes it with the value 8
, and assigns its address to myPointer
.