I. Basic Concepts
Malloc/free:
1. function prototype and description:
Void * malloc (long NumBytes): This function allocates NumBytes bytes and returns a pointer to this memory. If the allocation fails, a NULL pointer is returned ).
Void free (void * FirstByte): This function returns the space previously allocated with malloc to the program or the operating system, that is, this memory is released, so that it is free again.
2. Memory operations:
The parameter of the malloc function is to accept the number of memory bytes to be allocated. If the memory can meet the Request volume, it will return: pointing to the initial position of the allocated memory block
The free function releases the memory pointed to by the pointer (instead of the released pointer itself, the pointer itself will not be deleted). The pointer must point to the first address of the released memory space.
New/free:
1. events occurred during the operation:
There will be two events when new occurs: 1). The memory is allocated (through the operator new function) 2). Call one or more constructors to build objects for the allocated memory.
Two events occur during the delete operation: 1). Call one or more destructor for the memory to be released. 2) release the memory (through the operator delete function)
2. special applications:
When delete is used without parentheses, delete assumes that the deleted object is a single object. Otherwise, it is assumed that the object to be deleted is an array.
Therefore, if [] is used when new is called, [] is also used when delete is called. If [] is not used when new is called, therefore, [] should not be used for calling.
Ii. essential differences between malloc/free and new/delete:
1. malloc/free is in C/C ++Standard library functions, New/delete is C ++'sOperator
2. new automatically allocates space.
3. For custom objects, the use of maloc/free cannot meet the requirements of Dynamic Object Management. The constructor must be automatically executed when the object is created, and the Destructor must be automatically executed before the object is extinct. Since malloc/free is a library function rather than an operator and is not controlled by the compiler, it is impossible to impose the tasks of executing constructor and destructor on malloc/free. Therefore, C ++ needs a new operator that can allocate and initialize dynamic memory for objects, and a delete operator that can clean up and release the memory of objects-in short, new/delete can call the object construction and destructor to perform more detailed work on the memory, but malloc/free cannot.
3. Contact
Since the new/delete function completely covers malloc/free, why does C ++ retain malloc/free? C ++ programs often call C functions, while C Programs can only use malloc/free to manage dynamic memory. 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. Therefore, new/delete and malloc/free must be paired.
Iv. Examples
Void * malloc (size_t size );
Use malloc to apply for an integer-type memory with a length. 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 returned by malloc is void *. Therefore, you must explicitly convert the type when calling malloc and convert void * to the required pointer type.