Imagine I have a pointer to an array of integers and then I want to delete it like I do below:
int * numbers = new int[10];
delete[] numbers;
How does the delete operator knows where the array numbers ends to free that memory (since C++ does not keep track of the length of the array as far as I know)?
Thanks!
It can do it however it wants. There are two common ways:
The implementation may use an associative array of allocated pointers mapped to their sizes.
The implementation may allocate a few extra bytes at the beginning to store the size and pass a pointer into the block to the caller.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With