According to answers in this question : "How does delete[] "know" the size of the operand array?"
your allocator will keep track of how much memory you have allocated
and
How free knows how much memory to deallocate"
Yes, the memory block size is stored "somewhere" by malloc (normally in the block itself), so that's how free knows. However, new[]/delete[] is a different story. The latter basically work on top of malloc/free. new[] also stores the number of elements it created in the memory block (independently of malloc), so that later delete[] can retrieve and use that number to call the proper number of destructors
Does this mean that delete[] is independent on where the pointer points to? Is the following code valid or will it result in memory leaks?
void createArray(){
char* someArray = new char[20];
readData(someArray);
//Is this delete still valid after moving the pointer one position?
delete[] someArray;
}
char readData(char* &arr){
char value = *arr;
//Let it point to the next element
arr += 1;
return value;
}