I was reading about the delete
and delete[]
operators today and I can see that
to free the memory allocated to myChar
, I should call delete
and to free memory for myCharArray
I should use delete[]
.
However I have always thought of myChar
as a pointer to an array of size 1. So why is it that we use delete
in this case; and why do we need delete
at all? Couldn't we have gotten away with using delete[]
everywhere if myChar
is effectively an array of size 1?
char* myChar = new char;
char* myCharArray = new char[5];
delete myChar;
delete[] myCharArray;