malloc and free are standard library functions for c++/c languages, and new/delete are operators of C + +. They can all be used to request dynamic memory and free memory. Here's a look at their differences.
one, the object of operation is differentmalloc and free are standard library functions for c++/c languages, and new/delete are operators of C + +. For objects of non-intrinsic data types, light Malloc/free cannot meet the requirements of dynamic objects. Objects are automatically executed when they are created, and the destructor is executed automatically before the object dies. Because Malloc/free is a library function and not an operator, the task of executing constructors and destructors cannot be imposed on malloc/free, not within the compiler's control permissions.An
operator is an attribute of the language itself, it has a fixed semantics, and the compiler knows what it means. Just like +-*/, the compiler interprets the semantics and generates the corresponding code.
Library functions are library-dependent, without which there is no library, which is partly language-independent. In theory, the compiler does not know or care about the function, the compiler guarantees only the compiler function, and the code that calls the function when the parameter and return values conform to the syntax and generate the corresponding call function. But some of the high-level compilers in the real world will have special handling of some of the functions that come with the standard library.
second, in the use of differentThe prototype of the function malloc is as follows:
void * malloc (size_t size);
Use malloc to request a block length integer type of memory, the program is as follows:
int *p = (int *) malloc (sizeof (int) * length);
We should focus on two elements: "Type conversion" and "sizeof".
1.The
type of malloc return value is void *, so the type conversion is displayed when malloc is called, and void * is converted to the desired pointer type. 2.the Mallo function itself does not recognize what type of memory to request,
it only cares about the total number of bytes in memory . The prototype of the function free is as follows:
void free (void * memblock);
Why is the free function not as complex as the malloc function? This is because the type of the pointer p and the amount of memory it refers to is known beforehand, and the statement free (p) frees the memory correctly. If p is a null pointer, then free has no problem with P no matter how many times it is manipulated. If p is not a null pointer, then the free operation of P for two consecutive times causes the program to run incorrectly.
New/delete Points of Use:
Operator new is much simpler to use than the function malloc, for example
- int *P1 = (int *) malloc (sizeof (int) * length);
- int *p2 = new Int[length];
This is because new has built-in sizeof, type conversion, and type safety Check functionality . for objects that are not internal data types,new initializes the initialization work while creating the dynamic object . If an object has more than one constructor, the new statement can also have multiple forms.
If new creates an array of objects, then only the parameterless constructor of the object can be used . For example:
OBJ *objects = new obj[100]; Create 100 Dynamic objects
Cannot be written
OBJ *objects = new obj[100] (1);//create 100 dynamic objects while assigning the initial value 1
when releasing an array of objects with delete, be careful not to lose the symbol ' [] '. For example
- delete []objects; The right usage
- Delete objects; Incorrect use of
The latter is equivalent to delete objects[0], missing another 99 objects.
The difference between the two:
1. New automatically calculates the space that needs to be allocated, and malloc needs to manually calculate the number of bytes
2. New is type-safe, and malloc is not, for example:
- int* p = new Float[2]; Errors are indicated at compile time
- int* p = malloc (2*sizeof (float)); Cannot indicate error at compile time
New operator is made up of two steps, namely operator new and construct (construction)
3, operator new corresponds to malloc, but operator new can be overloaded, you can customize the memory allocation policy, even do not do memory allocations, or even allocate to non-memory devices. And malloc is powerless. 4, new will call constructor (constructor), and malloc cannot; Delete will call destructor (destructor), and free cannot. 5, Malloc/free to the library file support, New/delete do not.
Essential DifferencesMalloc/free is a standard library function for the C + + language, and New/delete is an operator of C + +. For user-defined objects, Malloc/free cannot meet the requirements of dynamic management objects. Objects are automatically executed when they are created, and the object executes the destructor automatically before it dies. Because Malloc/free is a library function and not an operator, the task of executing constructors and destructors cannot be imposed on malloc/free, not within the control of the compiler. Therefore, C + + requires an operator new that can perform dynamic memory allocation and initialization, and an operator delete that can perform cleanup and freeing of memory work.
classOBJ { Public: Obj () {cout<< "Initialization" <<Endl;} ~ Obj () {cout << "Destroy" <<Endl;} voidInitialize () {cout << "initialization" <<Endl;} voidDestroy () {cout << "Destroy" <<Endl;} }; voidUsemallocfree () {OBJ* A = (OBJ *)malloc(sizeof(OBJ));//Allocate MemoryA-Initialize ();//initialization//... ..A-Destroy ();//Deconstruction Free(a);//Release Memory} voidUsenewdelete (void) {OBJ* A =NewOBJ; //... .. DeleteA; }
The function of the class obj initialize realizes the function of the constructor function, and the function destroy realizes the function of the destructor. In function Usemallocfree, because Malloc/free cannot execute constructors and destructors, member functions initialize and destroy must be called to complete the construction and destruction.
So we do not use Malloc/free to complete dynamic object memory management, should use New/delete. Because the "objects" of the internal data types do not have a process of construction and destruction, Malloc/free and new/delete are equivalent to them.
Contact
Since New/delete's functionality completely covers the Malloc/free, why is C + + still reserved for malloc/free? Because C + + programs often have to call C functions, the program can only use Malloc/free to manage dynamic memory. If you release the dynamic object created by new with free, the object can cause a program error because it cannot execute the destructor. If you release "Dynamic Memory for malloc request" with delete, the program is theoretically not error-free, but the program is poorly readable. So New/delete,malloc/free must be paired with each other.
Transferred from: http://blog.sina.com.cn/s/blog_4d3a41f4010116ha.html
The difference between standard library functions and operators of C++/C languages New/delete Malloc/free