I know that you have to do it like this:
int * p;
p = new int[10];
//use array
delete [] p;
Now my question is: Since it’s not explicitly stated, how is it possible that the correct amount of memory is freed? Do the OS keep track of the allocated memory and its starting address?
The runtime library will keep track of allocated blocks of memory. It’s guaranteed to deallocate the block correctly, given the initial pointer returned by
new.While this can be implemented in the OS itself (theoretically), it normally isn’t. What the OS keeps track of is the pages allocated to a process as a whole, not individual blocks allocated at this level of abstraction.