Using the function malloc and free in C for memory management (allocation and deallocation), the operator new and delete are provided in C + + to do the same work, which is superior in performance and more convenient and flexible to use.
1.the basic form of new for memory allocation is:
pointer variable name =new type
while the program is running, new is A free storage area of the heap allocates a block of memory to the size of the type byte and stores the first address of the chunk memory in the pointer variable.
eg.
int *p; Declares a shaping pointer variable p
p=new int; Dynamically allocates a memory space that holds int data and assigns the first address to P
Delete pointer variable name
Delete p;
The operator new and delete functions are similar to the function malloc () and free (), but they have several advantages:
(1) operator new can automatically calculate the size of the type of memory to be allocated, rather than using the function sizeof () to calculate the required number of bytes, which reduces the likelihood of an error occurring.
(2) The operator new can automatically return the correct pointer type without forcing a type cast on the return pointer.
Here to Note:
(1) The space allocated by the operator new, should be used after the end of the operation can only be released with the operator delete, otherwise this part of the space will not be recycled and become dead space, if this has been new down, the system can cause paralysis.
(2) When allocating memory dynamically with operator new, if there is not enough memory to satisfy the allocation requirement, new returns a null pointer (null), so the dynamic allocation of memory is usually checked successfully.
(3) If you release the dynamic object created by new with free, the object cannot perform a destructor and cause the program to fail. Using Delete to release the dynamic memory of a malloc request is theoretically not an error, but the program is poorly readable.
eg.
int main ()
{
int *p;
p=new int;
if (!p)
{
cout<< "Allocation failure";
return 1;
}
*p=10;
cout<<*p;
Delete p;
return 0;
}
If you use operator new to dynamically allocate memory for an array, then add the size of the arrays after the type name.
For example:
int *p=new int[10];
If you are allocating space for a multidimensional array:
For example:
int *pi=new int[2][3][4];
When you delete with delete, you do not need to indicate the number of dimensions and sizes that are deleted. Simply precede the pointer with a square bracket that represents the operation of the array space.
eg. delete []pi;
The difference between new delete and malloc free:
1.new Delete is an operator in C + +, and malloc free is a function in C.
2.new not only allocates memory but also invokes the constructor of the class, delete calls the destructor, and malloc allocates only memory without initializing the class member's work, and free does not call the destructor.
The 3.new comes out with a pointer that is directly with type information, and malloc returns a void pointer.
This article is from the "Rookie Experience" blog, please be sure to keep this source http://10738432.blog.51cto.com/10728432/1748702
Resolution and differences of new, delete, and malloc, free