Today, I encountered a short answer at the Ericsson Nanjing R & D center: The difference between Delete P and delete [] P. The answer is poor. Next I will copy an article from the Internet.Article.
Operator new and operator Delete functions have two overloaded versions. Each version supports related new expressions and delete expressions:
Void * Operator New (Size_t ); // Allocate an object
Void * Operator New [] (Size_t ); // Allocate an array
Void Operator Delete ( Void *); // Free an oject
Void Operator Delete [] ( Void *); // Free an array
A friend familiar with C may find it strange here:
Free (void *) is used to release memory in C. [Note that there is only one parameter void *.] Why are there two in C ++! Why does Delete call free to release the memory?
In addition, how does Delete [] know the number of deleted objects?
In addition, general textbooks such as the "high quality c ++ programming guide" all say this:
When releasing an object Array Using Delete, do not lose the symbol '[]'. For example
Delete [] objects; // correct usage
Delete objects; // incorrect usage
The latter is equivalent to delete objects [0], and 99 Other objects are missing.
Of course, this description isErrorAnd mislead the audience
To solve the problem, open vc6 and enter the followingCode:
ClassA
{
Private:
IntI;
StringS;
Public:
~ A () {printf ("Hi");}
};
VoidD (*);
IntMain (IntArgc,Char* Argv [])
{
A * P =NewA [10];
D (P );
Return0;
}
VoidD (A * P)
{
Delete P;
}
Running result: Debug assertion failed!
Isn't it equivalent to delete p [0? In order to see the truth, I had to use the compilation I forgot many years ago. After some tossing, I finally came up with the following points:
1. If the object has no destructor (including the synthesis of destructor is not required, for example, comment out ~ A and string s lines of code)
Delete directly calls operator Delete and directly calls free to release the memory.
At this timeNew = new [] (only in quantity difference ),Delete = Delete [] 2 if the object has a destructor (including a compositing destructor), [This is the key ]:New [] will return 4 bytes after the return address, and use the 4 to store the array size! New does not need to move the four bytes behindDelete [] calls the destructor of a specified number of times based on the value of the four bytes. Likewise, delete does not need the four bytes. The result is that when you use Delete and delete [] to call free improperly, the four bytes may be misplaced, And the debug assertion is failed!Return to "high quality c ++ programming guide":
Delete [] objects; // correct usage
Delete objects; // incorrect usage
The latter is equivalent to delete objects [0], and the other 99 objects are missing. This should be strictly said:The latter is equivalent to calling only the objects [0] destructor, and missing the Destructor that calls the other 99 objects, in addition, an exception occurs when the memory is released after the call (if there is a destructor). If the object has no destructor, this statement is the same as Delete [] objects.Note: 1. incorrect opinion is not guaranteed in the test environment vc62. 3. Correction is welcome.
An array space allocated by new, for example, int * array = new int [50]. If delete is used to release this space, the statement Delete [] is equivalent to the delete array!
C ++ tells us that it is being recycledDelete is used for memory space of a single object allocated by new, and delete [] is used for memory space of a group of objects allocated by new [].
New [] and delete [] are divided into two types: (1) Allocating and reclaiming space for basic data types; (2) Allocating and reclaiming space for custom data types.
For (1),ProgramA can prove that Delete [] is equivalent to delete.
Program:
# Include <stdio. h>
# Define Buff_size 10240
Int Main ( Int Argc, Char * Argv [])
{
Printf ( " Hello, world \ n ") ;
Char * P = NULL;
While ( 1 )
{
P = New TTT [buff_size];
Printf ( " 0x % 08xh \ n " , P );
Sleep ( 5000 );
Delete P; // Or delete [] P;
P = NULL;
}
Return 0 ;
}
But for (2), the situation changes. See the following program.
# Include <stdio. h>
# Define Buff_size 10240
Class Ttt
{
Public :
TTT ()
{
// AA = new char [1024];
};
~ TTT ()
{
// Delete [] AA;
// Printf ("TTT destructor () \ n ");
};
Private :
Int A;
Char * AA;
Int INTA [ 1024 ];
};
Int Main ( Int Argc, Char * Argv [])
{
Printf ( " Hello, world \ n ") ;
TTT * P = NULL;
While ( 1 )
{
P = New TTT [buff_size];
Printf ( " 0x % 08xh \ n " , P );
Delete P; // Delete [] P;
P = NULL;
}
Return 0 ;
}
You can run this program on your own and check the different results of Delete P1 and delete [] p1. I will not paste the running results here.
From the running results, we can see that when Delete P recycles space, only P [0] calls the destructor, other objects such as P [1] and P [2] Do not call their own destructor. The memory release operation in the Destructor will not be executed (causing memory leakage ), memory usage continues to increase, which is the crux of the problem. If Delete [] is used, all objects will first call their own destructor before the space is reclaimed, and the memory used will not increase continuously.
There is no destructor for objects of the basic type. Therefore, you can use Delete and delete [] to recycle the array space composed of the basic types. However, you can only use Delete [] for arrays of class objects. For a single new object, only delete can be used, and delete [] cannot be used to recycle space.
I tested it and it seems that there is no difference. I can't remember where I can use Delete []. Let's comment.
# Include <iostream>;
# Include " Xercesc/DOM. HPP "
Int Main (){
Char * Pc = 0 ;
Char * PC2;
Int I =21 ;
PC = New Char ;
STD: cout <( Long ) Pc <STD: Endl;
Delete PC;
STD: cout <( Long ) Pc <STD: Endl;
PC2 = New Char ;
STD: cout <( Long ) PC2 <STD: Endl;
Return 0 ;
}
Output:
[Root @ ts XML] #./A. Out
134519536
134519536
134519536
The address is not changed. Delete [] and delete are all the same.
Therefore, a simple usage principle is that new corresponds to delete, new [], and delete.
Address: http://blog.csdn.net/ssfp8762/article/details/4783458