What is the difference between malloc/free and new/delete in C++
This article introduces the relevant knowledge of "what is the difference between malloc/free and new/delete in C++". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Problem description
In C++, it is generally possible to use malloc/free and new/delete to dynamically request and free memory.
Malloc/free
The free () function is used to free the memory allocated by the malloc () function and is called in pairs with malloc ().
The argument of the free () function is of pointer type. When releasing, you need to determine whether the pointer is NULL or not.
The free () function releases memory and does not call the destructor of the pointer object.
Free () and malloc () are operations on memory space
New/delete
Delete is used to free memory allocated by new, which is called in pairs with new
Delete does not need to judge whether the following object is NULL or not
Delete not only frees memory, but also calls the object's destructor.
Delete and new are operations on objects and are operators
problem
If you forget to call delete or because it is not called in other ways, the memory allocated in the heap will not be automatically reclaimed, resulting in a memory leak; now you can generally choose to use smart pointers, which are now supported by the cantileaked 11 standard.
This is the end of the content of "what's the difference between malloc/free and new/delete in C++". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!