前言
我们已经掌握的内存开辟方式是在栈空间上开辟空间或者连续的空间。
在栈上开辟空间的方式有两个特点:
1、 空间开辟大小是固定的。
2.、数组在申明的时候,必须指定数组的长度,它所需要的内存在编译时分配。
但当对于空间的需求,需要的空间大小在程序运行的时候才能知道,那数组的编译时开辟空间的方式就不能满足。这时候就只能使用动态存开辟。
一、malloc函数
(一)、malloc函数介绍
malloc函数原型:void* malloc (size_t size);
malloc函数,动态内存分配函数,用于申请一块连续的指定大小的内存块区域以void*类型返回分配的内存区域地址,分配大小是程序要求的大小,即动态分配。
(二)、malloc函数使用细节
malloc函数使用细节:
- 如果开辟成功,则返回一个指向开辟好空间的指针。
- 如果开辟失败,则返回一个NULL指针,因此malloc的返回值一定要做检查。
- 返回值的类型是 void* ,所以malloc函数并不知道开辟空间的类型,具体在使用的时候使用者自己来决定。
- 如果参数 size 为0,malloc的行为是标准是未定义的,取决于编译器。
C语言提供了另外一个函数free,专门是用来做动态内存的释放和回收的,函数原型如下:void free (void* ptr);free函数用来释放动态开辟的内存。只要是用动态内存函数,最后就要使用free函数释放。
free函数使用细节:
- 如果参数 ptr 指向的空间不是动态开辟的,那free函数的行为是未定义的。
- 如果参数 ptr 是NULL指针,则函数什么事都不做。
(三)、malloc函数具体使用
如下代码:
#include <stdlib.h>
#include<stdio.h>
int main()
{
int arr[10] = { 0 };
int* ptr = NULL;//先初始化为空
ptr = (int*)malloc(10*sizeof(int));//申请5个空间
if (NULL != ptr)//判断ptr指针是否为空
{
int i = 0;
for (i = 0; i<10; i++)
{
*(ptr + i) = 0;//置为0
}
}
for (int i = 0; i<10; i++)
{
printf("%d ", arr[i]);//打印5个书
}
free(ptr);//释放ptr所指向的动态内存
ptr = NULL;
return 0;
}
内容解析看注释。
结果如下:
二、calloc函数
(一)、calloc函数介绍
calloc函数原型:void* calloc (size_t num, size_t size);
calloc在内存的动态存储区中分配num个长度为size的连续空间,函数返回一个指向分配起始地址的指针,如果失败,返回NULL。
(二)、calloc使用细节和具体使用
calloc函数使用细节:
- 函数的功能是为 num 个大小为 size 的元素开辟一块空间,并且把空间的每个字节初始化为0。
- 函数 malloc 的区别只在于 calloc 会在返回地址之前把申请的空间的每个字节初始化为全0。
具体使用:
#include <stdlib.h>
#include<stdio.h>
int main()
{
int* ptr = NULL;
ptr = (int*)calloc(5,20);//申请5个空间大小,且初始化为0
if (NULL != ptr)//判断ptr指针是否为空
{
for (int i = 0; i<5; i++)
{
printf("%d ", ptr[i]);打印结果
}
}
free(ptr);//释放ptr所指向的动态内存
ptr = NULL;
return 0;
}
内容解析看注释。
结果如下:
三、realloc函数
(一)、realloc函数介绍
realloc函数原型:void* realloc (void* ptr, size_t size);
realloc函数的出现让动态内存管理更加灵活。如果过去申请的空间太小了,或者觉得申请的空间过大了,为了合理的时候内存,要对内存的大小做灵活的调整。那 realloc 函数就可以做到对动态开辟内存大小的调整。
(二)、realloc使用细节和具体使用
realloc函数使用细节:
- ptr 是要调整的内存地址
- size 调整之后新大小
- 返回值为调整之后的内存起始位置。
- 这个函数调整原内存空间大小的基础上,还会将原来内存中的数据移动到新的空间。
具体使用:
#include <stdlib.h>
#include<stdio.h>
int main()
{
int* ptr = NULL;
ptr = (int*)malloc(20);
if (NULL != ptr)//判断ptr指针是否为空
{
for (int i = 0; i<5; i++)
{
*(ptr + i) = i;
}
}
int *p = (int*)realloc(ptr, 40);
if (NULL != p)
{
ptr = p;
for (int i =5; i<10; i++)
{
*(ptr + i) = i;
}
}
for (int i = 0; i<10; i++)
{
printf("%d", ptr[i]);
}
free(ptr);//释放ptr所指向的动态内存
ptr = NULL;
return 0;
}
内容解析:
第一步:先用malloc函数申请5个空间,判断是否为空,不为空,则置数。
第二步:用realloc函数,找到刚才初始地址,它会把前五个拷贝过来再此基础上申请5个,,一共10个,40个字节。如果不为空,把p赋给ptr,再置数。
第三步:打印10个数。
(三)、realloc调整内存空间存在的情况
有两种情况:
第一种是:原有空间之后有足够大的空间。要扩展内存就直接在原有内存之后直接追加空间,原来空间的数据不发生变化。
第二种:原有空间之后没有足够大的空间。有空间之后没有足够多的空间时,扩展的方法是:在堆空间上另找一个合适大小的连续空间来使用。这样函数返回的是一个新的内存地址。
用realloc函数尽量使用第二种情况
第一种情况举例:
当申请很少时直接追加
#include <stdlib.h>
#include<stdio.h>
int main()
{
int* ptr = NULL;
ptr = (int*)malloc(20);
if (NULL != ptr)//判断ptr指针是否为空
{
for (int i = 0; i<5; i++)
{
*(ptr + i) = i;
}
}
ptr = (int*)realloc(ptr, 40);//申请新的空间
for (int i =5; i<10; i++)
{
*(ptr + i) = i;
}
for (int i = 0; i<10; i++)
{
printf("%d", ptr[i]);
}
free(ptr);//释放ptr所指向的动态内存
ptr = NULL;
return 0;
}
第二种情况举例:
当申请很多时再找一个合适的内存空间:
#include <stdlib.h>
#include<stdio.h>
int main()
{
int* ptr = NULL;
ptr = (int*)malloc(20);
if (NULL != ptr)//判断ptr指针是否为空
{
for (int i = 0; i<5; i++)
{
*(ptr + i) = i;
}
}
int*p = (int*)realloc(ptr, 1000);//申请新的空间
if(NULL!=p)
{
ptr=p;
for (int i =5; i<250; i++)
{
*(ptr + i) = i;
}
for (int i = 0; i<250; i++)
{
printf("%d", ptr[i]);
}
}
free(ptr);//释放ptr所指向的动态内存
ptr = NULL;
return 0;
}
四、常见的动态内存错误
(1)、对NULL指针的解引用操作
void test()
{
int *p = (int *)malloc(INT_MAX/4);
*p = 20;//如果p的值是NULL,就会有问题
free(p);
}
(2)、对动态开辟空间的越界访问
void test()
{
int i = 0;
int *p = (int *)malloc(10*sizeof(int));
if(NULL == p)
{
exit(EXIT_FAILURE);
}
for(i=0; i<=10; i++)
{
*(p+i) = i;//当i是10的时候越界访问
}
free(p);
}
(3)、对非动态开辟内存使用free释放
void test()
{
int a = 10;
int *p = &a;
free(p);//ok?
}
(4)、使用free释放一块动态开辟内存的一部分
void test()
{
int *p = (int *)malloc(100);
p++;
free(p);//p不再指向动态内存的起始位置
}
(5)、对同一块动态内存多次释放
void
test()
{
int*p= (int*)malloc(100);
free(p);
free(p);//重复释放
}
(6)、动态开辟内存忘记释放(内存泄漏)
void test()
{
int*p= (int*)malloc(100);
if(NULL != p)
{
*p= 20;
}
}
int main()
{
test();
while(1);
}
五、内存经典题解析
一、
void GetMemory(char*p)
{
p= (char*)malloc(100);
}
void Test(void)
{
char*str=NULL;
GetMemory(str);
strcpy(str, "hello world");
printf(str);
}
解析:内存泄漏,程序崩了,因为 GetMemory 并不能传递动态内存,而且 GetMemory 里面的空间也释放不了; Test 函数中的 str 一直都是NULL。 strcpy(str, “hello world”);将使程序崩溃。
二、
char *GetMemory(void)
{
char p[] = "hello world";
return p;
}
void Test(void)
{
char *str = NULL;
str = GetMemory();
printf(str);
}
解析:可能是乱码,因为 GetMemory 返回的是指向“栈内存”的指针,该指针的地址不是 NULL,但其原现的内容已经被清除,新内容不知道。
三、
void GetMemory2(char **p, int num)
{
*p = (char *)malloc(num);
}
void Test(void)
{
char *str = NULL;
GetMemory(&str, 100);
strcpy(str, "hello");
printf(str);
}
解析:能够输出 hello ,但没有释放内存,会造成内存泄漏 。
四·、
void Test(void)
{
char *str = (char *) malloc(100);
strcpy(str, “hello”);
free(str);
if(str != NULL)
{
strcpy(str, “world”);
printf(str);
}
}
解析:改变了动态内存区的内容,后果难以预料,非常危险。 因为 free(str);之后,str 成为野指针, if(str != NULL)语句不起作用。