1) Not the same type
Malloc/free is the standard library function for C/s + + language : Can overwrite, need library file support, not within compiler control permission, can not impose the task that executes constructor and destructor to Malloc/free;
New/delete is a C + + operator: can be overloaded, is reserved word, so no header file required
2) Manage memory, return value, operation content is not the same
malloc: Allocates memory, returns a pointer to the memory (void type, requires a specified memory size ), and malloc is requesting the memory space of the heap area .
Free: Releases the memory pointed to by the pointer, does not delete the pointer itself, and the pointer points to garbage memory, so the pointer is pointing to NULL.
new:1). Memory is allocated (via the operator new function, the free store memory space is requested, a pointer with type is returned, and the memory size can be calculated automatically )
2). Call one or more constructors to build the object for the allocated memory (can be initialized by the passing of it)
DELETE:1). Call one or more destructors for the memory being freed
2). Free memory (via operator delete function); To point the pointer to null.
3) Delete p or free (p), p becomes a dangling pointer (no defined pointer, invalid pointer)
If p is a null pointer, then Free/delete does not have a problem with P no matter how many times it is manipulated.
If p is not a null pointer, then free/delete to P for two consecutive times will cause the program to run an error: The first time p becomes a pointer to the garbage memory, the second time must be a problem
4) Why does C + + still retain malloc/free?
Because C + + programs often have to call C functions, and a program can only use Malloc/free to manage dynamic memory
See more Blogs
Use examples from blogs
void * malloc (size_t size);
Use malloc to request a block length integer type of memory, the program is as follows:
We should focus on two elements: "Type conversion" and "sizeof".
1. The type of malloc return value is void *, so the type conversion is explicitly performed when malloc is called, and the void * is converted to the desired pointer type.?
The 2.malloc function itself does not recognize what type of memory to request, it only cares about the total number of bytes in memory.
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 two consecutive times causes the program to run an error: the first time free (p) turns p into a pointer to garbage memory, and the second call to free (p) will definitely be problematic
Key points of use of New/delete
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 you create an array of objects with new, you can only use the parameterless constructor of the object. For example
When releasing an array of objects with delete, be careful not to lose the symbol ' [] '. For example
delete []objects; Correct usage of delete objects; The incorrect usage of the latter is equivalent to delete objects[0], which misses another 99 objects.
New/delete and Malloc/free