我们已经掌握的内存开辟方式有:
int val = 20;//在栈空间上开辟四个字节
char arr[10] = {0};//在栈空间上开辟10个字节的连续空间 但是上述的开辟空间的方式有两个特点:
但是对于空间的需求,不仅仅是上述的情况。有时候我们需要的空间大小在程序运行的时候才能知道,那数组的编译时开辟空间的方式就不能满足了。 C 语言引入了动态内存开辟,让程序员自己可以申请和释放空间,就比较灵活了。
但是我们很明显就看出来了这样创建的内存空间相对来说比较死板,有可能多了会造成内存空间的浪费,也有可能少了会造成内存空间不够用。
C 语言提供了一个动态内存开辟的函数:
void* malloc (size_t size);功能:向内存的堆区申请一块连续可用的空间,并返回指向这块空间的起始地址。
NULL 指针,因此malloc的返回值一定要做检查。void* ,所以malloc函数并不知道开辟空间的类型,具体在使用的时候时候使用者自己来决定。size 为 0,malloc的行为是标准未定义的,取决于编译器。#include<stdio.h>
#include<stdlib.h>
int main()
{
//假设申请20个字节的空间,存放5个整数
int* p = (int*)malloc(5 * sizeof(int));
//判断返回值
if (p == NULL)//开辟失败
{
perror("malloc");//打印错误信息
return 1;
}
//开辟成功了,使用这块空间
for (int i = 0; i < 5; i++)
{
*(p + i) = i + 1;
}
for (int j = 0; j < 5; j++)
{
printf("%d ", p[j]);
}
return 0;
}C 语言提供了另外一个函数free,专门是用来做动态内存的释放和回收的,函数原型如下:
void free (void* ptr);free函数用来释放动态开辟的内存。
ptr 指向的空间不是动态开辟的,那free函数的行为是未定义的。ptr 是NULL指针,则函数什么事都不做。malloc和free都声明在 <stdlib.h> 头文件中。
举个例子:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int num = 0;
scanf("%d", &num);
int arr[num];
//注意这里是C99中的变长数组,变长数组是不可以初始化的
//变长数组的num一旦确定,大小就不能改变了
int* ptr = NULL;
ptr = (int*)malloc(num * sizeof(int));
if (ptr != NULL)//依旧判断指针是否为空
{
for (int i = 0; i < num; i++)
{
*(ptr + i) = 0;
}
}
//释放ptr所指向的动态内存
//free完如果不主动给指针置为空,他就是野指针
free(ptr);
ptr = NULL;
return 0;
}C 语言还提供了一个函数叫 calloc,calloc 函数也用来动态内存分配。原型如下:
void* calloc (size_t num, size_t size);num 个大小为 size 的元素开辟一块空间,并且把空间的每个字节初始化为 0。malloc 的区别只在于 calloc 会在返回地址之前把申请的空间的每个字节初始化为全 0。举个例子:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int* p = (int*)calloc(10, sizeof(int));
if (p != NULL)
{
for (int i = 0; i < 10; i++)
{
printf("%d ", *(p + i));
}
}
free(p);
p = NULL;
return 0;
}
所以如果我们对申请的内存空间的内容要求初始化,那么可以很方便的使用calloc函数来完成任务。
先说结论:malloc就是申请空间并且不初始化;calloc就是申请空间并且初始化
接下来我们通过俩段非常相似的代码来进行分析:
#include<stdio.h>
#include<stdlib.h>
int main()
{
//申请十个整型的空间
int* p = (int*)calloc(10, sizeof(int));
if (p == NULL)
{
perror("calloc");
return 1;
}
//使用
for (int i = 0; i < 10; i++)
{
printf("%d\n", p[i]);
}
//释放空间
free(p);
p = NULL;
return 0;
}#include<stdio.h>
#include<stdlib.h>
int main()
{
//申请十个整型的空间
int* p = (int*)malloc(10 * sizeof(int));
if (p == NULL)
{
perror("malloc");
return 1;
}
//使用
for (int i = 0; i < 10; i++)
{
printf("%d\n", p[i]);
}
//释放空间
free(p);
p = NULL;
return 0;
}
realloc 函数就可以做到对动态开辟内存大小的调整。函数原型如下:
void* realloc (void* ptr, size_t size);ptr 是要调整的内存地址size 调整之后新大小,单位是字节
情况 1 当是情况 1 的时候,要扩展内存就直接原有内存之后直接追加空间,原来空间的数据不发生变化。 情况 2 当是情况 2 的时候,原有空间之后没有足够多的空间时,扩展的方法是:在堆空间上另找一个合适大小的连续空间来使用。这样函数返回的是一个新的内存地址。
我们会发现情况一就是在一块普通空间后面申请一块空间追加在一起即可,但是情况二就比较特殊了,所以我为大家总结了一下:
1、将旧的空间数据拷贝一份到新的空间 2、返回新的空间的起始地址 3、释放掉旧的地址
当然了realloc也可以直接创建一块新的空间:
#include<stdio.h>
#include<stdlib.h>
//realloc函数也可以直接用来申请空间
int main()
{
//穿空指针代表我们用来开辟空间
int* p = (int*)realloc(NULL, 40);
return 0;
}我们再举一个比较简单的realloc的例子:
#include<stdio.h>
#include<stdlib.h>
int main()
{
//申请一块内存空间用来存放1~5
int* p1 = (int*)malloc(5 * sizeof(int));
if (p1 == NULL)
{
perror("malloc");
return 1;
}
//使用内存空间
for (int i = 0; i < 5; i++)
{
p1[i] = i + 1;
}
//继续存放6~10
//内存空间不够,需要增容
int* p2 = realloc(p1, 10 * sizeof(int));
if (p2 == NULL)
{
perror("realloc");
free(p1);
p1 = NULL;
return 1;
}
p1 = p2;
//接下来可以继续使用p1来维护新的空间
for (int i = 5; i < 10; i++)
{
p1[i] = i + 1;
}
free(p1);
p1 = NULL;
return 0;
}void test()
{
int* p = (int*)malloc(INT_MAX / 4);
*p = 20;//如果p的值是NULL,就会有问题
free(p);
}我们要知道一般在开辟完空间并且赋值的时候我们要判断指针是否为空指针,这个错误顾名思义就是:没有对malloc/calloc/realloc函数的返回值做判断。
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);
}这还是很简单就可以看出来的,我们在写代码的时候要尽量避免这种问题的出现。
void test()
{
int a = 10;
int* p = &a;
free(p);//ok?
}free只能释放动态即malloc/calloc/realloc开辟的内存空间,而这段代码中的int *p = &a;是在栈区上空间是自动开辟的,也会自动回收
void test()
{
int* p = (int*)malloc(100);
p++;
free(p);//p不再指向动态内存的起始位置
}free释放空间的时候一定要给这段空间的起始位置。
void test()
{
int* p = (int*)malloc(100);
free(p);
free(p);//重复释放
}我们再来看一个也是重复释放的例子:
#include<stdio.h>
#include<stdlib.h>
int* test()
{
int* p = (int*)malloc(10 * sizeof(int));
if (p == NULL)
{
perror("malloc");
return 1;
}
//...
free(p);
return p;
}
int main()
{
int* ptr = test();
free(ptr);//二次释放了
ptr = NULL;
return 0;
}错误点分析
test() 函数内部已释放内存,但返回了悬空指针
free(p) 释放了动态分配的内存,但函数仍然返回了 p,此时 p 是一个 悬空指针(Dangling Pointer),指向的内存已被系统回收。
main() 函数中对同一块内存二次释放
ptr 接收了 test() 返回的悬空指针,再次调用 free(ptr) 会导致 未定义行为(Undefined Behavior),可能引发程序崩溃或安全漏洞。
当然我们还可以看到他返回了一个局部变量的地址,但是在我们前面的学习中我们应该知道这种行为也是错误的,那这里为什么没有说他错呢?
因为 malloc 分配的内存是 堆内存(Heap Memory),而不是局部变量(栈内存)。
p 是合法的(但必须确保后续正确释放,否则会内存泄漏)。
void test()
{
int* p = (int*)malloc(100);
if (NULL != p)
{
*p = 20;
}
}
int main()
{
test();
while (1);
return 0;
}忘记释放不再使用的动态开辟的空间会造成内存泄漏。 切记:动态开辟的空间一定要释放,并且正确释放。
总结: 1、谁申请的空间谁释放 2、不使用的空间及时释放 3、自己(函数1)不方便释放的空间要告诉别人(函数2)释放
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void GetMemory(char* p)
{
p = (char*)malloc(100);
}
void Test(void)
{
char* str = NULL;
GetMemory(str);
strcpy(str, "hello world");
printf(str);
}
int main()
{
Test();
return 0;
}请问运行Test函数会有什么样的结果?
答案:存在内存泄漏的问题,同时对空指针进行了解引用,程序奔溃
解析:首先这里的printf(str)等价于printf("%s",str);我们来看Test函数内部,从上到下给代码标为1、2、3、4,第一步给str置为空指针;第二步调用函数,p接收了str传来的空指针,然后在堆区上申请了100个字节的空间,将起始地址放入p中,覆盖NULL;第三步调用函数strcpy,上述步骤均不影响str,我们再来看strcpy函数,strcpy(char* dest, const char* src);这里面俩个指针均不能为空,因为拷贝需要进行解引用操作。
改善:
方案一:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void GetMemory(char** p)
//传过来的是一个一级指针的的地址,我们要调用二级指针来接收
{
*p = (char*)malloc(100);
//相当于
//str=(char*)malloc(100);
}
void Test(void)
{
char* str = NULL;
GetMemory(&str);//我们传入一个指针变量的地址(地址的地址)
strcpy(str, "hello world");//这里的str便不再是NULL
printf(str);
//释放
free(str);
str = NULL;
}
int main()
{
Test();
return 0;
}方案二:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* GetMemory(char* p)
{
p = (char*)malloc(100);
return p;
}
void Test(void)
{
char* str = NULL;
GetMemory(str);
strcpy(str, "hello world");
printf(str);
//释放
free(str);
str = NULL;
}
int main()
{
Test();
return 0;
}#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;
}请问运行Test函数会有什么样的结果?
答案:打印了一堆随机值
解析:局部变量栈区上的数组,出了作用域会销毁
改善:
#include<stdio.h>
#include<stdlib.h>
char* func(void)
{
static char p[] = "hello world";
//static修饰的数组是存放在内存的静态区的
//出了函数也不会被销毁
return p;
}
void test(void)
{
char* str = NULL;
str = func();
printf(str);
}
int main()
{
test();
return 0;
}#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void GetMemory(char** p, int num)
{
*p = (char*)malloc(num);
}
void Test(void)
{
char* str = NULL;
GetMemory(&str, 100);
strcpy(str, "hello");
printf(str);
}
int main()
{
Test();
return 0;
}请问运行Test函数会有什么样的结果?
答案:没有释放,但是可以打印hello
解析:这里很明显就可以看出来是str忘记释放
改善:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void GetMemory(char** p, int num)
{
*p = (char*)malloc(num);
}
void Test(void)
{
char* str = NULL;
GetMemory(&str, 100);
strcpy(str, "hello");
printf(str);
//释放
free(str);
str = NULL;
}
int main()
{
Test();
return 0;
}#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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函数内部的代码从上到下标注为1、2、3、4,第一步创建一块动态内存大小为100字节,强转为char*类型将str的初始地址存入其中;第二步将hello放入这100个字节的空间中;第三步将str指向的空间释放了,即100的那块空间还给操作系统了,但str中依旧存放着起始地址并没有置为NULL;第四步就是使用了野指针
改善:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void Test(void)
{
char* str = (char*)malloc(100);
strcpy(str, "hello");
free(str);
str = NULL;
if (str != NULL)
{
strcpy(str, "world");
printf(str);
}
}
int main()
{
Test();
return 0;
}也许你从来没有听说过柔性数组(flexible array)这个概念,但是它确实是存在的。 C99 中,结构中的最后一个元素允许是未知大小的数组,这就叫做『柔性数组』成员。、
例如:
struct st_type
{
int i;
int a[0];//柔性数组成员
};有些编译器会报错无法编译可以改成:
struct st_type
{
int i;
int a[];//柔性数组成员
};sizeof 返回的这种结构大小不包括柔性数组的内存。malloc() 函数进行内存的动态分配,并且分配的内存应该大于结构的大小,以适应柔性数组的预期大小。如果直接创建变量,他是不会给柔性数组的成员分配空间的
typedef struct st_type
{
int i;
int a[0];//柔性数组成员
}type_a;
int main()
{
printf("%d\n", sizeof(type_a));//输出的是4
return 0;
}#include <stdio.h>
#include <stdlib.h>
typedef struct st_type
{
int i;
int a[0];//柔性数组成员
}type_a;
int main()
{
printf("%d\n", sizeof(type_a));//输出的是4
return 0;
}
int main()
{
int i = 0;
type_a* p = (type_a*)malloc(sizeof(type_a) + 100 * sizeof(int));
//业务处理
p->i = 100;
for (i = 0; i < 100; i++)
{
p->a[i] = i;
}
free(p);
return 0;
}这样柔性数组成员a,相当于获得了100个整型元素的连续空间。
上述的 type_a 结构也可以设计为下面的结构,也能完成同样的效果。
#include <stdio.h>
#include <stdlib.h>
typedef struct st_type
{
int i;
int* p_a;
}type_a;
int main()
{
type_a* p = (type_a*)malloc(sizeof(type_a));
p->i = 100;
p->p_a = (int*)malloc(p->i * sizeof(int));
//业务处理
int i;
for (i = 0; i < 100; i++)
{
p->p_a[i] = i;
}
//释放空间
free(p->p_a);
p->p_a = NULL;
free(p);
p = NULL;
return 0;
}上述 代码1 和 代码2 可以完成同样的功能,但是 方法1 的实现有两个好处:
第一个好处是:方便内存释放 如果我们的代码是在一个给别人用的函数中,你在里面做了二次内存分配,并把整个结构体返回给用户。用户调用
free可以释放结构体,但是用户并不知道这个结构体内的成员也需要free,所以你不能指望用户来发现这个事。所以,如果我们把结构体的内存以及其成员要的内存一次性分配好了,并返回给用户一个结构体指针,用户做一次free就可以把所有的内存也给释放掉。 第二个好处是:这样有利于访问速度 连续的内存有益于提高访问速度,也有益于减少内存碎片。(其实,我个人觉得也没多高了,反正你跑不了要用做偏移量的加法来寻址)

C/C++ 程序内存分配的几个区域:
static)存放全局变量、静态数据。程序结束后由系统释放。

这题大家自行看知识点二和三熟悉并掌握即可
使用malloc函数模拟开辟一个3*5的整型二维数组,开辟好后,使用二维数组的下标访问形式,访问空间。
#include<stdio.h>
#include<stdlib.h>
int main()
{
int** p = (int**)malloc(3 * sizeof(int*));
for (int i = 0; i < 3; i++)
{
p[i] = (int*)malloc(5 * sizeof(int));
}
//进行初始化
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 5; j++)
{
p[i][j] = 5 * i + j;
}
}
//打印
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 5; j++)
{
printf("%d ", p[i][j]);
}
printf("\n");
}
//释放
for (int i = 0; i < 3; i++)
{
free(p[i]);
}
free(p);
p = NULL;
return 0;
}