In the VC, create an object with new NEW or if the operation fails does not throw an exception, but just returns a NULL pointer. There is the following code:
Delete P; // P is a legitimate pointer delete P;
This code will give a warning when the VC's Debug version is running the second line, because the P pointer points to a legally assigned memory area, and the delete is unpredictable. Also, C promises that Delete a NULL pointer is safe. I tracked into the VC's runtime library to see, DELETE operation When the passing parameter is null, it will not be directly returned.
There is also a code:
LPVOID P; P = Heapalloc (getProcessheap (), Heap_zero_memory, 10); delete P;
This code will also occur when executed to a third statement. Because the memory block allocated with the API function Heapalloc and the type of memory block allocated with the new operator is different, the information registered by the memory management system is also different, so remember: the memory assigned by HeapAlloc () must be Release with HeapFree (), and the memory allocated with the New must be released with Delete, absolutely not mixed. Another code:
Char * p; p = new char [10]; delete (p );
This code is implemented in a third line, and (p ), although it points to legitimate allocated memory addresses, it is not the first address of this memory block, and the memory management system cannot release this memory block according to it. The result is also unpredictable.
There is also a situation:
Const char * p; p = new char [10]; delete p;
This code will report an error in the compile period. Because the parameter type of the delete operator is a void *, the compiler cannot convert the const pointer to a non-Const pointer, so error. There have been problems: (MyClass is a class defined class) (1): lpvoid p; p = new myclass [10]; delete [] p; (2): lpvoid p; p = new myclass [10]; delete P; (3): lpvoid p; p = new myclass [10]; delete (myclass *) p); (4): myclass * p; p = new myclass [10]; delete p; above 4) When running, there is an error, and the Output window will have Memory Check Error. (5): myclass * p; p = new myclass [10]; delete [] p; (6): lpvoid p; p = new myclass [10]; delete [] ((myclass *) P); above The code is running correctly. (7): lpvoid p; p = new char [10]; delete p; (8): lpvoid p; p = new char [10]; delete [] P;
The above two code run does not have any error messages. Conclusion, NEW has an array of objects to release them with the delete [] operator. Because the NEW object array is to store more information than the New A separate object, the organizational structure of the allocated memory block is different, and the delete [] requires the correct object type information, and the VOID * pointer is very dangerous, must Explicitly transform it into a correct type of pointer. For the built-in type, such as int, char, etc., it seems that there is no limit, I don't know how the compiler of the VC is.