文章目录
1、为什么存在动态内存空间的开辟
一般说到开辟内存,我们不难想到下面的两种方式。
int a=10;//在栈空间上开辟4个字节的空间
int arr[10]={0};//在栈空间开辟40个字节的空间
但是这样看起来很笨的样子,我们需要去提前知道需要开辟多少的空间,可有时候只有在运行程序的时候才能知道,所以就出现了动态内存开辟,需要多少,让系统自己开辟多少即可。
2、动态内存函数的介绍
(1)malloc和free
malloc函数向内存申请一块连续可用的空间
如果申请成功,则返回这块空间的指针。
如果开辟失败,就返回空指针。
由于返回值是void*,所以在使用的时候需要强制类型转化。
注:如果参数为0,malloc行为是标准未定义的,取决于编译器。
因为动态内存开辟了以后,除了程序结束以后,被操作系统自动释放以外,只能通过free来释放,如果不及时释放就可能造成内存泄漏的问题。
free这个函数就是用来释放动态开辟的内存。
若参数是指向的空间不是动态内存开辟的,那free函数的行为是未定义的。
若参数是NULL指针,则函数什么事情都不需要做。
例:
#include <stdio.h>
#include <stdlib.h>
int main()
{
//开辟内存空间
int* p=(int*)malloc(10 * sizeof(int));
if (p == NULL)
{
perror("malloc");
return;
}
for (int i = 0; i < 10; i++)
{
*(p+i) = i;
}
for (int i = 0; i < 10; i++)
{
printf("%d ", p[i]);
}
//释放内存空间
free(p);
p = NULL;
return 0;
}
(2)calloc
这个函数也是开辟动态内存空间,需要传的参数为num和size
num是指元素的数量大小。
size指一个元素所需要的大小
这个函数与malloc的区别就是,这个函数不需要初始化,直接都是0;
例·:
int main()
{
//开辟内存空间
int* p = (int*)calloc(10 , sizeof(int));
if (p == NULL)
{
perror("calloc");
return;
}
for (int i = 0; i < 10; i++)
{
*(p + i) = i;
}
for (int i = 0; i < 10; i++)
{
printf("%d ", p[i]);
}
free(p);
p = NULL;
return 0;
}
(3)realloc
realloc函数是基于malloc和calloc的基础上的,在使用了前两者后,发现内存好像还需要在开辟,就需要使用realloc函数
ptr是指向要调整的内存大小
size是调整后的新大小
返回值是调整后的内存起始位置
这个函数在调整内存空间大小的基础上,还好将原来内存的数据迁移到新空间上。
使用:
当然也可传空指针
这个函数扩容的时候会有两种情况:
3、常见的动态内存错误
(1)对NULL指针的解引用的操作
int main()
{
int *p = (int*)malloc(40);
//不做返回值判断,就可能使用NULL指针,解引用
*p = 20;
return 0;
}
(2)对动态开辟空间的越界访问
int main()
{
int* p = calloc(10, sizeof(int));
if (p == NULL)
{
perror("calloc");
return 1;
}
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);
p = NULL;
return 0;
}
(3)对非动态开辟内存使用free释放
int main()
{
int a = 10;
int* p = &a;
//释放
free(p);
p = NULL;
return 0;
}
(4)使用free释放一块动态开辟内存的一部分
int main()
{
int* p = calloc(10, sizeof(int));
if (p == NULL)
{
perror("calloc");
return 1;
}
int i = 0;
for (i = 0; i < 5; i++)
{
*p = i;
p++;
}
//0 1 2 3 4 0 0 0 0 0
//释放
free(p);
p = NULL;
return 0;
因为p的位置已经发生了变化,所以释放的时候仅仅只是释放了后面的一部分,前面的并没有,所以被程序会奔溃。
(5)对同一块动态内存多次释放
int main()
{
int* p = malloc(40);
if (p == NULL)
{
//....
return 1;
}
//....
//释放
free(p);
p = NULL;
//...
free(p);
return 0;
}
(6)动态开辟内存忘记释放(内存泄漏)
void test()
{
int* p = (int*)malloc(40);
//...
if (3)
return;
free(p);
p = NULL;
}
int main()
{
test();
//...
while (1)
{
;
}
return 0;
}
4、经典的面试题
(1)
void GetMemory(char *p)
{
p = (char *)malloc(100);
}
void Test(void)
{
char *str = NULL;
GetMemory(str);
strcpy(str, "hello world");
printf(str);
}
请问运行Test 函数会有什么样的结果?
如何达到我们的目的呢
#include <stdlib.h>
#include <string.h>
//方法1:
char* GetMemory()
{
char* p = (char*)malloc(100);
return p;
}
void Test(void)
{
char* str = NULL;
str=GetMemory();
strcpy(str, "hello world");
printf(str);
free(str);
str = NULL;
}
//法2:
void GetMemory(char** p)
{
*p = (char*)malloc(100);
}
void Test(void)
{
char* str = NULL;
GetMemory(&str);
strcpy(str, "hello world");
printf(str);
free(str);
str = NULL;
}
int main()
{
Test();
return 0;
}
int main()
{
Test();
return 0;
}
(2)
char *GetMemory(void)
{
char p[] = "hello world";
return p;
}
void Test(void)
{
char *str = NULL;
str = GetMemory();
printf(str);
}
请问运行Test 函数会有什么样的结果?
(3)
void GetMemory(char **p, int num)
{
*p = (char *)malloc(num);
}
void Test(void)
{
char *str = NULL;
GetMemory(&str, 100);
strcpy(str, "hello");
printf(str);
}
请问运行Test 函数会有什么样的结果?
(4)
void Test(void)
{
char* str = (char*)malloc(100);
strcpy(str, "hello");
free(str);
if (str != NULL)
{
strcpy(str, "world");
printf(str);
}
}
int main()
{
Test();
return 0;
}
请问运行Test 函数会有什么样的结果?
5、C/C++程序的内存开辟