0
点赞
收藏
分享

微信扫一扫

【C语言】动态内存分配

Python芸芸 2022-04-23 阅读 73
c语言

 malloc and free    一定要一起使用

malloc 

开辟连续内存空间,单位为字节,eg:malloc(20) 为开辟 20 个字节的空间 

 malloc 向内存申请 连续可用 的空间 ,并返回这段空间的指针

返回类型为 void* 类型 ,malloc 不知道开辟空间类型,所以需要对返回值强制类型转换

eg:(int*)

 开辟成功:返回开辟的空间指针

开辟失败:返回NULL指针,malloc 分配空间需要进行判断是否为 NULL

size 为 0 时是未定义的,取决于编译器

#include<stdio.h>
#include<stdlib.h>//malloc
#include<errno.h>//errno
#include<string.h>//strerror

int main()
{
    int* p = (int*)malloc(10 * sizeof(int));//(int*)强制类型转换成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 ", *(p + i));
        }
    }
    //释放空间
    free(p);//释放p的内存
    p = NULL;//将p定为空指针
    return 0;
}

free

 free 函数是用来释放动态开辟的内存

 ptr 指向的空间不是动态开辟的,free 函数的行为是未定义的

ptr 是 NULL指针,则函数什么都不做


calloc

 与 malloc 相似

区别在于calloc 在返回地址之前将申请空间的每个字节初始化为 0 


 realloc

 调整动态开辟内存空间大小

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
int main()
{
    //malloc 开辟 20 个字节
    int* p = (int*)malloc(20);
    
    if (p == NULL)
    {
        printf("%s\n", strerror(errno));
    }
    else
    {
        int i = 0;
        for (i = 0; i < 5; i++)
        {
            *(p + i) = i;
        }
    }
    //假设 20 个字节不足
    //开辟 40 个字节
    int* p2 = (int*)realloc(p, 40);//动态开辟内存
    int i = 0;
    for (i = 0; i < 10; i++)
    {
        printf("%d ", *(p2 + i));
    }
    /*free(p);
    p = NULL;*/
    return 0;
}

 realloc 函数使用注意事项:如果新的地址较大,则新分配部分的值不确定


错误:越界访问

 错误:对非动态开辟内存使用 free 释放

错误: 对 NULL指针 解引用操作

 错误:对同一块动态内存多次释放

 可以将 p 置为 NULL,这样第二个free(p),就没有影响

 错误:对动态开辟的内存空间忘记内存释放(内存泄露)

错误:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

void GetMemory(char* p)
{
    p = (char*)malloc(100);
}

void test()
{
    char* str = NULL;
    GetMemory(str);
    strcpy(str, "hello world");
    printf(str);
}

int main()
{
    test();
    return 0;
}

str 以值的形式传给 p 

p 是GetMemory 函数的形参,只在函数内部有效,GetMemory 函数返回之后,动态开辟内存未释放,且无法找到,造成内存泄漏

改法1: 

 GetMemory 函数参数里传 str 的地址,GetMemory 函数二级指针接受str指针的地址

*p 开创空间

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

void GetMemory(char** p)
{
    *p = (char*)malloc(100);
}

void test()
{
    char* str = NULL;
    GetMemory(&str);
    strcpy(str, "hello world");
    printf(str);
    free(str);
    str = NULL;
}

int main()
{
    test();
    return 0;
}

改法2:

 GetMemory 函数返回类型改为 char* ,函数返回 p 的指针

str 就是GetMemory 函数返回的指针

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

char* GetMemory(char* p)
{
    p = (char*)malloc(100);
    return p;
}

void test()
{
    char* str = NULL;
    str = GetMemory(str);
    strcpy(str, "hello world");
    printf(str);
    free(str);
    str = NULL;

}

int main()
{
    test();
    return 0;
}

返回栈空间地址问题:

局部变量在栈区,出局部变量所在的函数时 ,局部变量会被销毁,如果在其他函数内进行调用,将返回值赋给别的内容时,就会出现找不到局部变量的情况

 错误:

p[] 为局部变量 ,在栈区,出了 GetMemory 函数 ,p 就被销毁,str 接受 p ,但无法找到 p 的内容 

#include<stdio.h>
#include<stdlib.h>
char* GetMemory(void)
{
    char p[] = { "hello world" };
    return p;
}

void test(void)
{
    char* str = NULL;
    str = GetMemory();
    printf(str);
}

int main()
{
    test();
    return 0;
}

修改:

#include<stdio.h>
#include<stdlib.h>
char* GetMemory(void)
{
    static char p[] = { "hello world" };//p[] 放到静态区,不在栈区
    return p;
}

void test(void)
{
    char* str = NULL;
    str = GetMemory();
    printf(str);
}

int main()
{
    test();
    return 0;
}

堆区可以返回

malloc 开辟的空间在堆区,不进行 free 空间依然存在, ptr 是局部变量,出Test函数后会销毁,但销毁前函数返回了ptr,

#include<stdio.h>
#include<stdlib.h>
int* Test()
{
    int *prt = malloc(100);
    return prt;
}

int main()
{
    int *p=Test();
    *p = 20;
    printf("%d", *p);
    return 0;
}

被回收的空间再次使用问题

 错误:

 开辟的空间被 free 释放,再次对 ptr 进行访问就是非法访问

free 释放后的开辟空间不会被置为 NULL空指针

free 后要将 ptr 置为 NULL,再进行判断(是否为 NULL)可以避免非法访问

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void Test()
{
    char* ptr = (char*)malloc(40);
    strcpy(ptr, "hello");
    free(ptr);
    if (ptr != NULL)
    {
        strcpy(ptr, "world");
        printf(ptr);
        
    }
}

int main()
{
    Test();
    return 0;
}

修改:

 free 后将 ptr 置为 NULL

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void test()
{
    char* ptr = (char*)malloc(40);
    strcpy(ptr, "hello");
    free(ptr);
    ptr = NULL;
    if (ptr != NULL)
    {
        strcpy(ptr, "world");
        printf(ptr);

    }
}

int main()
{
    test();
    return 0;
}
举报

相关推荐

0 条评论