赞
踩
malloc and free 一定要一起使用
开辟连续内存空间,单位为字节,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 函数是用来释放动态开辟的内存
ptr 指向的空间不是动态开辟的,free 函数的行为是未定义的
ptr 是 NULL指针,则函数什么都不做
与 malloc 相似
区别在于:calloc 在返回地址之前将申请空间的每个字节初始化为 0
调整动态开辟内存空间大小
int* p = (int*)realloc(NULL,40); //效果等同于 malloc
- #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 函数使用注意事项:如果新的地址较大,则新分配部分的值不确定
p=realloc(p,40);
1、当 p 指向的空间之后有足够的空间追加则追加,后返回 p
2、当 p 指向的空间之后没有足够的空间追加,则realloc会重新找一个新的内存空间,开辟满足需求的空间,把原来的内存中的数据拷贝回来,释放旧的内存空间,返回重新开辟的内存空间地址
3、使用的一个新的变量接受 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;
- }
- #include<stdio.h>
- #include<stdlib.h>
- typedef struct stu
- {
- int n;
- int arr[];//柔性数组大小可以调整,大小可以不写,也可以写成arr[0],
- }S;
-
- int main()
- {
- //
- S* ps = (S*)malloc(sizeof(S) + 10 * sizeof(int));
- ps->n = 100;
- int i = 0;
- for (i = 0; i < 5; i++)
- {
- ps->arr[i] = i;
-
- }
- S* ptr = realloc(ps, 44);
- if (ptr != NULL)
- {
- ps = ptr;
- }
- for (i = 5; i < 10; i++)
- {
- ps->arr[i] = i;
- }
- for (i = 0; i < 10; i++)
- {
- printf("%d ", ptr->arr[i]);
- }
-
- free(ps);
- ps = NULL;
- return 0;
- }
malloc 分配空间几次,就要 free 释放几次
先 free 后分配的内存空间
- #include<stdio.h>
- #include<stdlib.h>
- typedef struct stu
- {
- int* arr;
- }S;
- int main()
- {
- S* ps = (S*)malloc(sizeof(S));
- ps->arr = malloc(5 * sizeof(int));
- int i = 0;
- for (i = 0; i < 5; i++)
- {
- ps->arr[i] = i;
- }
- for (i = 0; i < 5; i++)
- {
- printf("%d ", ps->arr[i]);
- }
- int* ptr = realloc(ps->arr, 10 * sizeof(int));
- if (ptr != NULL)
- {
- ps->arr = ptr;
- }
- for (i = 5; i < 10; i++)
- {
- ps -> arr[i] = i;
- }
- printf("\n");
- for (i = 0; i < 10; i++)
- {
- printf("%d ", ps->arr[i]);
- }
- //释放
- //malloc 两次
- //先释放 ps->arr,在释放 ps
- free(ps->arr);
- ps->arr = NULL;
- free(ps);
- ps = NULL;
- return 0;
- }
柔性数组的优势:
1、方便内存释放
2、利于访问速度
注意:
1、柔性数组成员前面至少一个其他成员
2、sizeof 返回的结构大小不包括柔性数组
3、malloc 分配大小,分配大小大于结构体大小,适应柔性数组
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。