[Resume questions] Getting started with C++: Use char* p = new char[100] to apply for a piece of memory, and then use delete p to release it. What's the problem?

Use char* p = new char[100] to apply for a section of memory, and then use delete p to release it. What's the problem?
A. There will be memory leaks
B. There will be no memory leaks, but it is not recommended to use
C. Compilation will report an error, you must use delete []p
D. Compile is no problem, it will crash directly when running

Analysis:
When delete is used to release the memory space applied for with new int[], since it is a basic data type without a destructor, using delete and delete [] is the same, both of which will release the memory space applied for
. For the data type, when there is a destructor, the space applied for with new [] must be released with delete [], because when deleting [], the destructor of the object array will be called one by one, and then the space will be released.
So the answer is B

Guess you like

Origin blog.csdn.net/m0_46613023/article/details/114867406