当前位置:网站首页>Application of C language dynamic memory function

Application of C language dynamic memory function

2022-06-22 01:30:00 51CTO

Application of dynamic memory allocation function

The dynamic memory allocation functions are :malloc,calloc,realloc and free, Here is how they are used

1.malloc function

malloc The function is used to apply for a specified byte size of space from the heap , The function parameter is the byte size of the requested space , Return value is one void* The pointer to , If the application fails, it will return NULL, The usage is as follows :

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int* p = (int*)malloc(4*sizeof(int));// Apply for one 4 individual int Type size space and return the address to a int* Type to receive 
    int i = 0;
    if(p == NULL)
    {
        perror("The following error occurred");//p If null pointer , Print reason 
        return 0;
    }

    for(i=0; i<4; i++)
    {
        *(p+i) = i;
        printf("%d ", *(p+i));
    }
    free(p);// Release the opened dynamic memory space , And the operating system 
    p= NULL;// hold p The pointer is set to null , Prevent safety problems 
    return 0;
}

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.

2.calloc function

calloc Functions and malloc Function similarity , It is also used to apply to the heap to open up memory space and return the address ,calloc The first parameter of the function is the number of elements stored in the application space , The second parameter is the byte size of the element type , Function return value type is void*, Besides calloc The function initializes each element of the space as 0, The usage is as follows :

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int* p = (int*)calloc(4, sizeof(int));//calloc The first parameter of the function is the number of elements , The second parameter is the byte size of each element 
    int i = 0;
    if(p == NULL)
    {
        perror("the error is: \n");// Failed to apply for space. Printing reason 
        return 0;
    }
    for(i=0; i<4; i++)
    {
        printf("%d \n", *(p+i));//calloc When the function applies for space, it initializes each element as 0
    }
    free(p);// Release p The dynamic space that memory points to 
    p = NULL;// take p Set to empty , Prevent illegal access 
    return 0;
}

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.

realloc function

realloc Function is a very important function in dynamic memory allocation , It can adjust malloc and calloc The size of the requested space , Make it more flexible ,realloc The first argument to the function is a pointer to a dynamic memory space , The second parameter is the modified space size , The return value is the modified space address , Use as follows :

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int* p = (int*) malloc(4*sizeof(int));
    int i = 0;
    if(p == NULL)
    {
        perror("the error is: \n");
        return 0;
    }
    for(i=0; i<4; i++)
    {
        *(p+i) = i;
        printf("%d ", *(p+i));
    }
    printf("\n");
    int* ptr = (int*) realloc(p, 8*sizeof(int));// Adjust the size of the requested dynamic memory space and return the adjusted space address , Use the new variable to receive the return value 
    if(p != NULL)
    {
        p = ptr;
        for(i=0; i<4; i++)
        {
            printf("%d ", *(p+i));// After the space adjustment, the contents stored in the original space remain unchanged 
        }
        for(i=4; i<8; i++)
        {
            *(p+i) = i;// Store content for new space 
            printf("%d ", *(p+i));
        }
    }
    free(p);
    p = NULL;
    return 0;
}

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.

realloc Function considerations :

1. If p There is enough memory after the space pointed to can be added directly , Return address after .
2. If p There is not enough memory space to append after the space pointed to , be realloc Will find a new memory area to open up a space to meet the needs , And copy back the data in the original memory , Free up old memory space , Finally, return the newly opened memory space address .
3. To receive... With a new variable realloc The return value of the function

4.free

free The function should be related to malloc/calloc Functions are used in pairs , It is by releasing space , However, the released pointer is not set to null. The pointer still has the ability to find the originally opened dynamic memory space, which will only cause illegal access , So use it again free When you free up space, set the pointer to NULL. Besides free The released memory space must be dynamically opened up ,free A null pointer means nothing .

原网站

版权声明
本文为[51CTO]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206220033505259.html