Superficial knowledge on the function malloc and free

  This article describes the contents of malloc and free functions. 

  In C, memory management is very important. The following describes these functions began: 

 

  a, the malloc () and free () the basic concepts and basic usage: 

1 , and description function prototype: 

void * the malloc ( Long NumBytes): This function NumBytes bytes allocated, and returns a pointer pointing to this memory. If the allocation fails, it returns a null pointer (NULL). 

On the causes allocation failure, there should be many, such as the lack of space is a kind. 

void  as Free ( void * FirstByte): This function is before the space allocated by malloc returned to the program or operating system, which is released this memory, let it regain freedom. 

 

2 , use the function: 

     In fact, these two functions with them not that difficult, that is, after malloc () think that using it is enough to dump it to free (), and give a simple example: 

program code: 
        // ... Code 
        char * the Ptr = NULL; 
        the Ptr = ( char *)the malloc ( 100 * the sizeof ( char )); 
         IF (== NULL the Ptr) 
     { 
          Exit ( . 1 ); 
    } 
        the gets (the Ptr); 

        // code ... 
        Free (the Ptr); 
        the Ptr = NULL; 
         // code ... 

That's it! Of course, the specific circumstances of the specific analysis and specific solutions. For example, you define a pointer in a function and then apply for a piece of memory returned by a function pointer passed to this, then maybe release this memory of this work should be left to the other functions. 

 

3 , in some places on the use of function Note: 

A, after application memory space, you must check whether the assignment was successful. 

B, when re-use is not required to apply memory recall release; should be released after this memory pointer to point to NULL, the careless use later in the program to prevent it. 

C, these two functions should be paired. Release is not a memory leak if the application; if for no reason and that is to release nothing has been done. Released only once, if the release of more than twice and twice

An error occurred (null pointer exceptions release, also released a null pointer actually equal to had nothing to do, so the release of a null pointer release many times, no problems). 

D, although the type of function malloc () is a ( void *), of any type can be converted into a pointer ( void * ), but it is best carried out in front of the cast, because it can avoid some of the compiler checks. 

Now enter the second part: 

 
  two, malloc () in the end from where come the memory: 

1 , malloc () in the end got the memory space where to? The answer is to get the space from inside the reactor. That function returns a pointer to a memory of the heap. The operating system has a linked list of free memory address of record. When the operating system receives the application program, it will traverse the linked list, and then find the first space is greater than the application heap space node, and then remove the node from the free nodes link list, and the junction point of space allocated to the program. Knowledge heap it can query the knowledge of the structure of the data or query previous post from C / C ++ heap, stack and static data area Comments [Reserved]. Here, however much introduction. 

 

2 , after use malloc () to allocate memory space, you must remember to free up memory space, otherwise there will be a memory leak. 



3 , free () release in the end what 

free() Is a pointer to release the memory! note! Release the memory, not the pointer! Pointer has not been released, the pointer is still pointing to the original storage space. A pointer is a variable, is destroyed only when the program ends. After the release of the memory space, the original point of this space pointer still there! But now spam pointer is undefined, so that is rubbish. Thus, after the release of the memory pointer to NULL, the pointer back to prevent accidentally turn the dereferenced. 

 
  Three, malloc () and free () mechanism: 

in fact, a closer look at the free () function prototype, perhaps will find seem magical, as Free () function is very simple, only one parameter, as long as the point of application space pointer passed to free () the parameters to complete the work release! To track here to malloc () application problem. When the application is substantially greater than the amount of memory the application. Because the excess space is used to record management information for this memory. 

Most implementations of the allocated storage space than required to be slightly larger, the extra space for recording management information - the length of the allocated block, a pointer to the next allocated block and the like. This means that if a trailing wrote the allocated area, it will after a rewrite of management information. This type of error is catastrophic, but because such an error will not soon exposed, so it is difficult to find. The pointer to the allocated blocks of the pointer may be moved backward according to rewrite the management information block. 

malloc space () application is actually divided two spaces of different nature. A space is used to record management information, the other is the available space. And for recording the management information is actually a structure. In the C language, often used to record configuration information! See in this prototype the following structure: 

program code: 
   struct mem_control_block { 
     int is_available;    // This should normally be a first address space available, but the English word here has shown a marked space is available 
    int size;             // this is the size of the actual space 
    }; 

 

  therefore, as Free () is based on this structure information to release malloc () application space! The size of the two members of the structure I think it is the operating system of things. 
  Consider the following free () of source code 
   // code ... 
    
       void  Free ( void * PTR)   
    { 
            struct mem_control_block * Free ; 
             Free = PTR - the sizeof ( struct mem_control_block); 
             Free -> = is_available . 1 ; 
             return ; 
    }

 

  As malloc source code, are interested can look online to find!
Detailed malloc and free

The above is taken: https://www.cnblogs.com/hanyonglu/archive/2011/04/28/2031271.html

Encountered a small problem when doing the test, we added some understanding of malloc and free.

1. L = create_seqlist // Create a table linear

      L = (seqlist *)malloc(sizeof(seqlist))

2. insert_seqlist // loop insert elements

SUMMARY 3. show_seqlist (L) // print the linear form

4. clear_seqlist (L) // q Clear linear form

       free(L)

SUMMARY 5. show_seqlist (L) // print the linear form

 

 

 

 

 

And found clear running, was still able to print the contents of a linear table

Why, after a free, L pointer contents still, it has to change?

First malloc apply to the system memory, and to assign a memory address pointer L, then memory can operate by L, and this memory can not be accessed by the other pointer.
L is the release of free pointer memory, i.e., the usage rights of the application space returned malloc system, not release the pointer L, L means that the pointer is pointing to the address previously allocated piece of memory, but this memory can also be other pointer to access, modify. L is a pointer to a local variable, will be released at the end where the function is called, the return of the right to use the system, but the data pointer L space will not change unless it was modified, so call again show function, but also print out the order of the table of contents . To prevent unauthorized access, generally free after the NULL pointer will be set.

 For the procedure, the clear added L = NULL, NULL pointer is not set, because the pointer L also belong to the main function of the local variables, after the clear call, L point remains unchanged.

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/y4247464/p/11913633.html