+ 1
What does this code mean?
int* ptr = new int(27); *ptr = 34; ... delete ptr;
1 Answer
+ 7
int* ptr = new int(27);
dynamically allocate memory on the heap. (27) means it is initialized with 27.
Be aware that [27] and (27) are different. [27] would mean an uninitialized array with 27 elements.
So ptr points somewhere on the heap and that memory location contains the value 27.
*ptr = 34;
Assign 34 to the memory location pointed to by ptr.
delete ptr;
The memory on the heap is no longer needed and is deallocated. ptr no longer owns the memory.
Note that delete[] would be used if ptr was pointing to an array.