C language memory management specifications

First, the application memory

       1. It is recommended to use calloc application memory, try not to use malloc.

       After completion calloc dynamic memory allocation, memory is automatically initialized to zero, but does not initialize malloc, inside the garbage data is random data.

       2. The application memory size must be greater than 0.

    (1) using a length of 0 bytes for memory behavior it is not defined, when the application function return address referenced memory cause unpredictable errors may occur in the case of the application of zero length memory is necessary is determined, to avoid this situation .

        (2) negative for memory length, will be a negative number as a large unsigned integer, resulting in too large for memory failure occurs.

       3. Check whether to apply the memory request is successful, i.e., to check whether the returned pointer is NULL, i.e., it is zero.

Second, memory release 

        1. Application of certain memory needs to be released, there is one and only released once

        2. prohibit release or the function returns a non-dynamic application memory (stack memory, the function of temporary variables, etc.)

   3. After the release pointer must be a pointer points to a null pointer, otherwise the situation will appear wild pointer.

Transfer: https://www.cnblogs.com/lh218/p/5373907.html

 

Guess you like

Origin www.cnblogs.com/qiumingcheng/p/11408445.html