No, this is not safe, it can lead to some undesirable situations.
For example, some data ( int/char/float ...) may be in memory near the place where your pointer points. At that time, if you used delete [] , then it may try to delete this data as well, if it is an integer, and for other data types it leads to unexpected errors.
Example:
int *ptr = new int; int a = 20; char c = 'e'; *ptr = 10; delete [] ptr;
There is a possibility that the variables c and a may be stored near the place where the ptr pointer points. If a is stored next to it, then it deletes " a " also, if other data types are present, leads to unexpected results.
Therefore, it is recommended that you use delete[] only to delete the allocated memory using the new data type [] . Arranging this is useful.
Mahesh
source share