Malloc ()/Free () is a function in C/C ++. New/delete is the operator in C ++. They are all used for memory allocation and recovery. Generally, new and delete are recommended in C ++, instead of malloc and free. Malloc is a C function that can be used in C ++. New is introduced in C ++ and can be said to be an enhanced version of malloc (). For classes, using new can also automatically call the class constructor, which is very important in some cases. New Request Heap Memory, malloc () also fromHeapApply for memory (The pointer returned by the function is a piece of memory in the heap. The operating system has a linked list that records idle memory addresses. When the operating system receives a program application, it traverses the linked list and searches for the first heap node with a larger space than the requested space, then, the node is deleted from the idle node linked list and the space of the node is allocated to the program.). New allocates memory to the object, which can lead to the execution of constructor and the corresponding destructor (delete) will be executed before the class is destroyed. Malloc () does not know constructor and destructor. Without these features, it is best not to use them for object creation. In C, there is no object concept, O (operator _ operator) O. Example (in C ++ ): String * string1 = <string *> (malloc (10 * sizeof (string); // by default, the malloc function returns the void * type, which must be forcibly converted. String * string2 = new string [10]; The result is that string1 does point to enough space for 10 string objects, but these objects are not created in the memory (if any, their values will be random ). In other words, string1 is actually useless at all. Instead, string2 points to an array containing 10 fully constructed string objects. Each object can be safely used in any string read operation. Free (string1 ); Delete [] string2; // If [] is not added, only one destructor is called and the memory space of a string is released! Calling free () will release the memory directed by string1, but the string object in the memory will not call the destructor. The value of string1 is not changed, but it still points to the original place, free () only indicates that the system identifies the memory as available, but does not automatically assign null to the pointer or empty the memory. If the string object has allocated the memory as usual, all the memory will be lost (the objects in the memory still exist, but you cannot get a pointer to them, they cannot be used ). On the contrary, when you call delete on string2, each object in the array will call the Destructor before the memory is released, set the pointer to null, and clear the memory. New = memory allocation (malloc) + initialization (call constructor) Delete = call destructor + release memory (free) The free memory is a memory space pointed to by the pointer, which should be empty. The deleted memory actually contains data or objects. If you use free to release the "New Dynamic Object", this object may cause program errors because it cannot execute the destructor. If you use Delete to release the "dynamic memory applied by malloc", theoretically, the program will not go wrong, but the program is poorly readable. A good habit is to use new/delete and malloc/free pairs strictly. For example, your objects are small and applications often dynamically add and delete objects. After running the program for a period of time, many memory fragments will be generated. Therefore, you need to write your own memory management to prevent memory fragments. You may need a large block of memory by using malloc, and then create many objects as needed. When there are no objects in the entire memory block, you can free up the large memory block. In general, malloc is used when you do not need to call the constructor, while new is used. |