Delete and delete in C ++ []

xiaoxiao2021-03-06  56

Delete and delete in C []

Yesterday, I wrote an article about Delete and Delete [], and the people pointed out that my conclusion is wrong, then the result will only be in a specific compiler program. In order not to mislead everyone, the article is deleted. After returning home, I watched "Effective C ", I have read the book too much, although I forgot who the people are, I am still thank you. The correct point of view in "Effective C " will now be extracted as follows:

1. When you use New, there are two things happen. First, the memory is configured (transmitting the function operator new). Second, there will be a (or more) constructors to be called for this memory. When you use Delete, there are two things happen: one (or more) DEStructors are called for this memory, and then the memory is released (through function Operator Delete).

2. If you use Delete, DELETE assumes that the delete object is a single object. Otherwise, it is assumed that the delete object is a group.

3. String * StringPtr1 = new string;

String * stringptr2 = new string [100];

......

DELETE STRINGPTR1;

delete [] stringptr2;

If you use "[]" form to StringPtr1, the result is undefined. If you don't use the "[]" form in StringPtr2, the result is not defined. I still don't define the built-in, even if this type doesn't have DEStructors.

4. Therefore, the rules of the game are simple. If you use [] when calling New, you are also used when calling delete [] if you call New [], then you should not call use[].

转载请注明原文地址:https://www.9cbs.com/read-114712.html

New Post(0)