Simply learn about dynamic memory management in C language

Table of contents

1. malloc and free

1.1 malloc function

1.2 free function

2. calloc function

3. realloc function

3.1 Precautions for using the realloc function:

4. Common mistakes in dynamic memory allocation

4.1 Dereferencing NULL

4.2 Out-of-bounds access to dynamic memory allocation

4.3 Use the free function to open up space for non-dynamic memory

4.4 Use free to release part of the dynamically allocated memory

4.5 Multiple releases of the same dynamically allocated memory

4.6 Forgetting to release dynamically allocated memory


1. malloc and free

1.1 malloc function

The header file of the malloc function is stdlib.h

Function prototype:

void* malloc(size_t size)  //开辟内存块,size为要几个字节大小;

Example:

int main()
{
    //向内存申请10个整形空间;
    int* p=(int*)malloc(10*sizeof(int));  //返回值为申请的空间的地址,需要强制类型转换;
    if(p==NULL)
    {
        printf("%s\n",strerror(errno));  //会报出对应的错误信息,即为什么内存开辟失败;
    }
    else
    {
        //正常使用空间;
        int i=0;
        for(i=0;i<10;i++)
        {
            *(p+i)=i;
        }
        for(i=0;i<10;i++)
        {
            printf("%d\n",*(p+i));
        }
    }
    //当申请的空间不再使用的时候
    //应该还给操作系统
    return 0;
}

1.2 free function

The free function is specially used to reclaim and release dynamic memory;

The function prototype is:

void free(void* ptr);

The free function is used to release the memory allocated by dynamic memory.

If the space pointed to by the parameter ptr is not dynamically opened, the behavior of free is undefined;

· If ptr is a NULL pointer, the function does nothing;

Example:

int main()
{
    //向内存申请10个整形空间;
    int* p=(int*)malloc(10*sizeof(int));  //返回值为申请的空间的地址,需要强制类型转换;
    if(p==NULL)
    {
        printf("%s\n",strerror(errno));  //会报出对应的错误信息,即为什么内存开辟失败;
    }
    else
    {
        //正常使用空间;
        int i=0;
        for(i=0;i<10;i++)
        {
            *(p+i)=i;
        }
        for(i=0;i<10;i++)
        {
            printf("%d\n",*(p+i));
        }
    }
    //当申请的空间不再使用的时候
    //应该还给操作系统
    free(p);   //当程序结束时,动态内存开辟的空间也会还回去,free就是主动还回去
    p=NULL;  //free过后p并没有发生变化,还是指向开辟的那块空间,为了防止那块空间被再次利用,就将p赋值为空指针;
    //malloc 和 free 函数要成对使用;
    return 0;
}

2. calloc function

Open up an array in memory, the elements are initialized to 0;

Function prototype:

void* calloc(size_t num,size_t size)

Example:

int main()
{
    // malloc(10*sizeof(int));
    int* p=(int*)calloc(10,sizeof(int));//几个元素,每个元素多少个字节;
//开辟失败时,同样返回空指针
    if(p==NULL)
    {
        printf("%s\n",strerror(errno));
    }
    else
    {
        int i=0;
        for(i=0;i<10;i++)
        {
            printf("%d",*(p+i));  //打印结果都是0;
        }
    }
    //释放空间
    free(p);
}

The difference between malloc and calloc functions is that malloc is efficient but not initialized; calloc is inefficient but initialized;

3. realloc function

Function prototype:

void* realloc(void* ptr,size_t size); 

The emergence of the realloc function makes dynamic memory management more flexible

realloc can adjust the size of dynamically allocated memory

ptr is the memory address to be adjusted, size is the new size after adjustment, and the return value is the adjusted memory starting address. This function will also move the data in the original memory to the new one on the basis of adjusting the original memory space. Space.

Example:

int main()
{
    int* p=(int*)malloc(20);
    if(p==NULL)
    {
        printf("%s",strerror(errno));
    }
    else
    {
        int i=0;
        for(i=0;i<5;i++)
        {
            *(p+i)=i;
        }
    }
    //此时在使用malloc开辟的20个字节的空间;
    //假设这里,20个字节不能满足我们的需求
    //希望有40个字节空间
    //此时可以使用realloc来调整内存;
    int* ptr=(int*)realloc(p,40);
    if(ptr!=NULL)
    {
        ps=ptr;
         int i=0;
    for(i=0;i<10;i++)
    {
        printf("%d ",*(ps+i));
    }
    }
    free(ps);
    ps=NULL;
    return 0;
}

3.1 Precautions for using the realloc function:

 1. If there is enough memory space to add after the space pointed
 to by p, it will be added directly, and then p will be returned; 2. If there is not enough memory space to add after the space pointed to by p, realloc will find a new memory area to open up a new one Space that meets the requirements, and copy the data in the original memory, release the original memory space, and finally return the address of the newly opened memory space; 3.
 A new variable must be used to accept the return value of realloc

4. Common mistakes in dynamic memory allocation

4.1 Dereferencing NULL

Example:

int* p=(int*)malloc(40);
//这里是有可能会开辟失败的,这个时候p就是空指针,下面就是对空指针进行解引用;
int i=0;
for(i=0;i<10;i++)
{
    *(p+i)=i;
}

4.2 Out-of-bounds access to dynamic memory allocation

Example:

int *p=(int*)malloc(5*sizeof(int));
if(p==NULL)
{
    return 0;
}
else
{
    int i=0;
    for(i=0;i<10;i++)  //造成越界访问了,5个元素放了10个元素;
    {
        *(p+i)=i;
    }
}
free(p);
p=NULL;

4.3 Use the free function to open up space for non-dynamic memory

Example:

int main()
{
    int a=10;
    int* p=&a;
    *p=20;
    free(p);
    p=NULL;
}

4.4 Use free to release part of the dynamically allocated memory

Example:

int main()
{
    int *p=(int*)malloc(40);
    if(p=NULL)
    {
        return 0;
    }
    int i=0;
    for(i=0;i<10;i++)
    {
        *p++=i;
    }
    free(p);
    p=NULL;  //错误原因就是p指向的变成后面的空间了,++使得p指向地址以及发生改变;
}

4.5 Multiple releases of the same dynamically allocated memory

int main()
{
    int* p=(int*)malloc(40);
    if(p=NULL)
    {
        return 0;
    }
    free(p);  //用完后将p置为空指针,可以防止多次释放;
    //·····
    free(p);
    return 0;
}

4.6 Forgetting to release dynamically allocated memory

//内存泄漏
int main()
{
    while(1)
    {
        malloc(1);
    }
}

おすすめ

転載: blog.csdn.net/yss233333/article/details/124073044