1. Knowledge points
Three-Step walk: Application, release, pointer empty.
1.1malloc, free function
In the C language, the memory malloc function requests dynamic space, and the following shows its basic usage:
1 int *p = NULL; 2 p = (int *)malloc(sizeof(int); Application for 3free (p); // release, or it will cause a memory leak 4 p = NULL; // the pointer is empty, otherwise it becomes a wild pointer
(1) The dynamically allocated space comes from the team space, while the pointer itself is stored in the stack space as a local variable.
(2) malloc may sometimes apply for space failure, which returns NULL, so it needs to be judged.
(3) The space requested through malloc dynamic must be released through the free function, and the two functions appear in pairs. Otherwise, there will be less available space.
(4) After releasing through the free function, it is best to empty the pointer.
(5) The Malloc/free function application release process is actually the free space linked list is constantly updated.
1.2new, delete function
(1) The new and delete operators can be applied to both the base type and the custom type, and the new operator not only applies the space, but also initializes the constructor based on the supplied arguments, and the delete invokes the object's destructor before releasing the memory space, which new/ Delete is richer than malloc/free.
2. Questions about the common sense of 2.1malloc and free
The following statement is correct (D).
(A) Free will place the pointer blank//need to be manually empty
(B) After the return pointer of the malloc function moves, the free function automatically finds the first address and releases//cannot lose control of the first address, otherwise it cannot be freed
(C) The malloc function applies for n int space at a time, and it needs to loop n times to invoke the free release//malloc
(D) The space for malloc applications is on the heap
2.2 Returns an address of 64 integer times
Write two functions, Align64malloc and Align64free, respectively, for requesting space and freeing space, and requesting that the address of the application space return must be an integer multiple of 64.
Resolution: Add 64 bytes to the required space before it is guaranteed to have an address that is a multiple of 64, plus 4 bytes in front of the 64 byte space to ensure that there is a place to store the returned first address. As shown in the following table
| A |
4 bytes |
| B |
64 bytes |
| C |
N bytes |
1 void* ALIGN64MALLOC (intsize) {2 void*ptr =malloc(sizeof(int) *size + -+sizeof(void*));3 if(!ptr) {4 returnNULL;5 }6PTR = (Char*) (PTR) +sizeof(void*);//The storage space for the first address is reserved at the front .7 //The next step is to put the first address space in the space in front of the 64 integer fold.8*((int*)(((int) ptr+ --(int) ptr% -)-sizeof(void*)))=(int) PTR-sizeof(void*);//The equation to the right of the first address, void* is not allowed to add and subtract operations9 return(void*)((int) PTR + -- (int) PTR% -);Ten } One A voidAlgin64free (void*ptr) { - if(PTR) { - Free((void*)(*((void* *) PTR-1)) ;//void * cannot be added and reduced, the pointer can be converted into a pointer after the addition and subtraction the } -}2.3 Brief description of the difference between Malloc/free and New/delete
(1) Malloc/free is a library function provided by the C language, accessed through function calls, passing parameters and receiving return values, while New/delete is a C + + operator with its own set of syntax rules and operations.
(2) Malloc/free can be used only for basic data types, and New/delete not only for basic data types, but also for custom types in object-oriented.
(3) The malloc function returns the void* type, the program needs to display the required pointer type, and the new operator directly indicates the type and does not involve a type conversion problem.
(4) malloc is only responsible for applying for space and returning the first address; the new operator, in addition to requesting space, also invokes the constructor to initialize what the pointer points to; the free Korean is only responsible for freeing up space and identifying the space as free space; The delete operator also calls the object's destructor in addition to freeing up space.
(5) In fact, the latter covers all functions of the former, and the reason why the Malloc/free function is retained in C + + is to solve the compatibility problem and prevent errors in C + + when calling C functions containing malloc/free.
2.4 Brief description of the difference between delete and delete[]
Answer (1) when the elements of an array in new[] are basic types, the array space can be freed by delete and delete[];
(2) when an array element in new[] is a custom type, the array space can be freed only through delete[] (because the destructor of the first element is only called with delete).
It is strongly recommended to apply and release space in a fully paired way: New and delete paired, new[] and delete[] paired.
The following two examples illustrate:
1 //basic type, both can be2 //A3 int*i =New int[5];4 Deletei;5 //B6 int*i =New int[5];7 Delete[] I;
1 //custom type new[]/delete[] must be paired2 classTest {3 Private:4 Char*text;5 Public:6Test (intLenght = -) {7Text =New Char[lenght];8 }9~Test () {Ten Deletetext; Onecout <<"A destructor"<<Endl; A } - }; - theTest *a =Newtest[5]; - Delete[] A;//using Delete will make an error
C + + Programming Fundamentals (6) memory allocation