dynamic memory allocation function malloc

#include <stdio.h> 
#include <stdlib.h> Free the malloc // 
#include <WINDOWS.H> SLEEP // 
void MAIN1 () { 
    // int A [1024 * 1024 * 1000]; // array only processing a small number of data 
    int NUM = 100; 
    // int B [NUM]; size of the array must be clear, a NUM variable, can be changed at any time 
    // array of such memory allocation mechanism is called static allocation, the system is completed array using automatic recovery 

    // dynamic memory allocation 
/ * malloc and free two functions are provided in the C standard library for dynamic memory application and release, the malloc () function call format is substantially: 
    void * the malloc (unsigned int size); 
    size parameter is an unsigned integer, whereby the user controls the size of the application memory, when successful, the system will open a memory size is size-byte area for a program, and returns the address of the first area, 
    the user may utilize the address management and use of the memory block, if the request fails (such as memory size enough), returns a null pointer is NULL. 
    malloc () function returns the type void *, the value returned by its other types of pointer assignments must be explicit conversion. 
    size bytes in size only to apply, regardless of the memory block and the type of data stored in the application, and therefore, the length of the application shall be given by the programmer through memory "length × sizeof (type)" means, for example:
    * P = int (int *) the malloc (. 5 * the sizeof (int)); 
    as Free memory is released, e.g. Free (P) 
    * / 
        // p <+ pFloat conditions Fint is to be noted here, since the cycle, it is necessary to use p cycles, pFloat is constant, it should be written as p <p + fInt, so <p on both sides move together
    // enter a number, and the initialization data with a float, in the form: F 1.000,2.000 ... 
    float F; 
    Scanf ( "% F", & F); 
    void * = pVoid the malloc (F * the sizeof (float)); / / malloc returns a null pointer value is 
    a float pFloat * = (a float *) pVoid; 
   // the standard method 
    for (int I = 0; I <F; I ++) { 
        pFloat [I] = I +. 1; 
        the printf ( " % F,% P \ n-", pFloat [I], & pFloat [I]); 
    } 
    // Finger 
    the printf (" \ n-\ n-\ n-"); 
    a float * P = pFloat; 
    int Fint = (int) F ; 
    for (int I = 0; P <pFloat Fint +; ++ I, P ++) { 
    } 
    Free (pVoid);// release pVoid memory address, only one free, NULL pointer may be released several times 
} 
 void MAIN2 () {
        P = I +. 1 *; 
        the printf ( "% F, P% \ n-", * P, P); 
    a float F = 3.0f; 
    int A = (int) F; 
    the printf ( "% D,% F", A , a); //3,0.000000 a small integer, according to resolve% f, will print 0.0000000 
} 

//////////////////////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// //// 
void main4 () { 
    int n-; 
    Scanf ( "% D", & n-); 
    // pDouble double * = (double *) the malloc (n-* the sizeof (Double)); // initialize the malloc not 
    double * pDouble = (double *) calloc ( n, sizeof (double)); // calloc memory is automatically initialized to 0 
    for (I = 0.0 Double; I <n-; I ++) { 
        pDouble [(int) I] = I + 1.0; // i cast to int 
        the printf ( "% LF, P% \ n-", pDouble [(int) I], & pDouble [(int) I]);

    } 
} 

void main5 () { 
    int NUM; 
    The printf ( "Please INPUT The size of Array: \ n-"); 
    Scanf ( "% D", & NUM); 
    int * P = (int *) the malloc (NUM * the sizeof (int)); 
    IF (P == NULL ) / * error protection process, to see whether the memory allocation success * / 
    { 
        the printf ( "memory allocation fails, exit"); 
        return; 
    } 
    for (int I = 0; I <NUM; I ++) { 
        P [I] = I +. 1; 
        the printf ( "% D,% P \ n-", P [I], & P [I]); 
    } 



    the printf ( "IF you want to rezize The Array, Please INPUT A new new Number: \ n-"); 
    newNum int; 
    Scanf ( "% D", & newNum); 
    // to reallocate memory space has been allocated and copy the contents 
    // function takes two parameters realloc (): assigned memory address, the number of bytes reallocated 
    / / void * realloc (void * ptr ,size_t size )
    int *newP = (int *) realloc((void *)p,newNum);
    // redistribution newNum bytes of memory, and to copy the contents of the original address over a malloc of p 
    printf ( "after realloc, p: % d,% p \ n", * p, p); // p: 1,00030E58 
    the printf ( "After realloc, P +. 5:% D, P% \ n-", * (P +. 5), P +. 5); // After realloc, P +. 5: 13643,006D0E6C 
    // seems realloc after not automatically relieves p, p address but the same, except that the type of points to a null pointer 


    for (int i = num; i <newNum; ++ i) {// Note that because the first num elements have realloc copied, so from the start copying num 
        newp [I] = I +. 1; 
    } 
    // array to print a new assignment 
    for (int J = 0; J <newNum; J ++) { 
        the printf ( "% D,% P \ n-", newp [J], & newp [J]); 
    } 
    Free (P); // after the release of memory, the value of the pointer (address) does not change, but the type of cancel 
    Free (newp); 
} 

/ * 
Please INPUT The size of Array: 
. 3 
1,00020E58  
2,00020E5C
3,00020E60
The rezize you want to IF Array, INPUT A new new Number Please: 
. 6 
1,00020E58 can be seen that the memory space allocated to the new first address is the same, that can be of any old p pointer is followed by a piece of memory space remaining unoccupied space, so continue to the next assignment 
2,00020E5C If there is no space left behind the old pointer p, will find another area of memory, reallocation, the old space is automatically freed 
3,00020E60 
4,00020E64 
5,00020E68 
* 6,00020E6C / 

void main () { 
    int NUM; 
    the printf ( "Please INPUT The size of Array: \ n-"); 
    Scanf ( "% D", & NUM); 
    int * P = (int *) the malloc (NUM * the sizeof (int)); 
    iF (P == NULL) / * error protection process, to see whether the application is successful memory * / 
    { 
        the printf ( "memory allocation fails, exit"); 
        return; 
    } the else { 
        for (int I = 0; I <NUM; I ++) {
        } 
            P [I] = I +. 1;
            the printf ( "% D,% p \ n-", p [I], & p [I]); 
        the printf ( "before Free:% p \ n-", p); 
        Free (p); // Free before and after the p address It does not change 
        printf ( "the after as Free: the p-% \ the n-", the p-); 
        the p-= NULL; // software engineering specifications, after the release of the pointer to be set to NULL, you can avoid problems cited again and again after the release of the release of 
        printf ( "after free p [2]: % d \ n", p [2]); then // free, again referenced p [2] will be garbage data, an error will 

    } 

}

 

Guess you like

Origin www.cnblogs.com/luoxuw/p/11315196.html
Recommended