第一章:为什么存在动态内存分配
int main()
{
int a = 10;//在栈空间上开辟四个字节
int arr[10];//在栈空间上开辟10个字节的连续空间
//上述的开辟空间的方式有两个特点:
//1. 空间开辟大小是固定的。
//2. 数组在申明的时候,必须指定数组的长度,它所需要的内存在编译时分配。
//但是对于空间的需求,不仅仅是上述的情况。有时候我们需要的空间大小在程序运行的时候才能知道,
//那数组的编译时开辟空间的方式就不能满足了。这时候就只能试试动态存开辟了。
}
第二章:动态内存函数的介绍
2.1 malloc和free
C
语言提供了一个动态内存开辟的函数:
void* malloc (size_t size);
分配一个size字节大小的内存块,返回一个指向该块开头的指针。
这个函数向内存申请一块
连续可用
的空间,并返回指向这块空间的指针。
- 如果开辟成功,则返回一个指向开辟好空间的指针。
- 如果开辟失败,则返回一个NULL指针,因此malloc的返回值一定要做检查。
- 返回值的类型是 void* ,所以malloc函数并不知道开辟空间的类型,具体在使用的时候使用者自己来决定。
- 如果参数 size 为0,malloc的行为是标准是未定义的,取决于编译器。
//malloc函数
#include <stdio.h>
#include <stdlib.h> //动态内存函数
#include <string.h> //strerror函数
#include <errno.h> //errno
int main()
{
int arr[10] = { 0 };//这种方式是在栈区申请空间
//int* p = (int*)malloc(INT_MAX);//申请空间太大,会开辟失败,所以下方要判断
int* p = (int*)malloc(40);//这种方式是在堆区申请空间
if (p == NULL)
{
printf("%s\n", strerror(errno));
return 1;//C语言历史习惯返回0是正常返回,程序走到这说明出现问题,所以返回1表示异常
}
//使用内存
int i = 0;
for (i = 0; i < 10; i++)
{
*(p + i) = i;
}
for (i = 0; i < 10; i++)
{
printf("%d ", *(p + i));
}
//没有free
//并不是说内存空间就不回收了
//当程序退出的时候,系统会自动回收内存空间
return 0;
}
C语言提供了另外一个函数free,专门是用来做动态内存的释放和回收的,函数原型如下:
void free(void* ptr);
free函数用来释放动态开辟的内存。
- 如果参数 ptr 指向的空间不是动态开辟的,那free函数的行为是未定义的。
- 如果参数 ptr 是NULL指针,则函数什么事都不做。
//free函数
int main()
{
int arr[10] = { 0 };
int* p = (int*)malloc(40);
if (p == NULL)
{
printf("%s\n", strerror(errno));
return 1;
}
//使用内存
int i = 0;
for (i = 0; i < 10; i++)
{
*(p + i) = i;
}
for (i = 0; i < 10; i++)
{
printf("%d ", *(p + i));
}
free(p);//释放。只是将40个字节还个操作系统,但p的地址依然没变,所以要让p置成空指针
p = NULL;
return 0;
}
2.2 calloc
C语言还提供了一个函数叫 calloc , calloc 函数也用来动态内存分配。原型如下:
void* calloc (size_t num, size_t size);
- 函数的功能是为 num个大小为 size 的元素开辟一块空间,并且把空间的每个字节初始化为0。
- 与函数 malloc 的区别只在于 calloc 会在返回地址之前把申请的空间的每个字节初始化为全0。
int main()
{
int* p = (int*)calloc(10, sizeof(int));
if (p == NULL)
{
printf("%s\n", strerror(errno));//0 0 0 0 0 0 0 0 0 0
return 1;
}
//打印
int i = 0;
for (i = 0; i < 10; i++)
{
printf("%d ", *(p + i));
}
free(p);
p = NULL;
return 0;
}
//想初始化选择calloc;否则选择malloc
//calloc可以理解为malloc + memset
2.3 realloc
- realloc函数的出现让动态内存管理更加灵活。
- 有时会我们发现过去申请的空间太小了,有时候我们又会觉得申请的空间过大了,那为了合理的使用内存,我们一定会对内存的大小做灵活的调整。那 realloc 函数就可以做到对动态开辟内存大小的调整。
函数原型如下:
void* realloc (void* ptr, size_t size);
-ptr 是要调整的内存起始地址
-size 调整之后新大小
-返回值为调整之后的内存起始位置。
-这个函数调整原内存空间大小的基础上,还会将原来内存中的数据移动到新的空间。
- ptr 是要调整的内存地址
- size 调整之后新大小
- 返回值为调整之后的内存起始位置。
- 这个函数调整原内存空间大小的基础上,还会将原来内存中的数据移动到 新 的空间。
- realloc在调整内存空间的是存在两种情况:
- 情况1:原有空间之后有足够大的空间
-
情况 2 :原有空间之后没有足够大的空间
情况
1
当是情况
1
的时候,要扩展内存就直接原有内存之后直接追加空间,原来空间的数据不发生变化。
情况
2
当是情况
2
的时候,原有空间之后没有足够多的空间时,扩展的方法是:在堆空间上另找一个合适大小的连续空间来使用。这样函数返回的是一个新的内存地址。
由于上述的两种情况,
realloc
函数的使用就要注意一些。
int main()
{
int* p = (int*)malloc(40);
if (NULL == p)
{
printf("%s\n", strerror(errno));
return 1;
}
int i = 0;
for (i = 0; i < 10; i++)
{
*(p + i) = i + 1;
}
//扩容
//realloc申请空间时可能失败,失败返回空指针,所以不能用原先空间的指针直接接收地址
//p = realloc(p, 80);
int* ptr = (int*)realloc(p, 80);
if (NULL != ptr)
{
p = ptr;
}
//使用
for (i = 0; i < 10; i++)
{
printf("%d ", *(p + i));
}
free(p);
p = NULL;
//补充
//realloc(NULL, 40);//等价于malloc(40)
return 0;
}
第三章:常见的动态内存错误
3.1 对NULL指针的解引用操作
int main()
{
int* p = (int*)malloc(40);
//在对指针解引用操作前要对指针进行判断,示例如下
if (p == NULL)
{
return 1;
}
*p = 20;//如果p的值是NULL,就会有问题
free(p);
p = NULL;
return 0;
}
3.2 对动态开辟空间的越界访问
int main()
{
int* p = (int*)malloc(40);
if (NULL == p)
{
printf("%s\n", strerror(errno));
return 1;
}
int i = 0;
for (i = 0; i <= 10; i++)
{
p[i] = i;//越界访问
}
free(p);
p = NULL;
return 0;
}
3.3 对非动态开辟内存使用free释放
int main()
{
int a = 10;
int* p = &a;
//程序崩溃
free(p);
p = NULL;
return 0;
}
3.4 使用free释放一块动态开辟内存的一部分
int main()
{
int* p = (int*)malloc(40);
if (NULL == p)
{
return 1;
}
int i = 0;
for (i = 0; i < 5; i++)
{
*p = i;
p++;
}
free(p);//p没有指向malloc开辟空间的起始地址就释放,程序崩溃
p = NULL;
return 0;
}
3.5 对同一块动态内存多次释放
int main()
{
int* p = (int*)malloc(40);
free(p);
free(p);//p还存放着该空间的地址,再次释放程序崩溃。所以释放以后还需将p置为空指针
return 0;
}
3.6 动态开辟内存忘记释放(内存泄漏)
//场景一:
void test()
{
int* p = (int*)malloc(100);
int flag = 0;
scanf("%d", &flag);
if (5 == flag)
return;
//如果flag等于5,那么该函数就返回并结束,导致释放的代码没有被执行
//并且出了函数,指针p被销毁,导致再也找不到这块空间
free(p);
p = NULL;
}
//场景二:
int* test1()
{
int* p = (int*)malloc(100);
if (NULL == p)
{
exit(EXIT_FAILURE);
}
return p;
}
int main()
{
test();
int* ret = test1();//此函数只开辟空间,没有释放功能。如果下方忘记释放就会造成内存泄漏
return 0;
}
第四章:几个经典的笔试题
4.1 题目1:请问运行Test 函数会有什么样的结果?
void GetMemory(char* p)
{
//指针变量p是此函数的形参,只能在函数内部使用,出了函数就销毁。
//下方调用此函数完成后,p指针不在了。但是malloc开辟的空间还在,并且没有被free,程序也没有结束,所以内存泄漏。
p = (char*)malloc(100);
}
void Test(void)
{
char* str = NULL;
GetMemory(str);
//str传过去,但p是形参,它的改变不影响str,str依然为空指针
//把hello world拷贝进空指针程序崩溃(也就是对空指针进行了解引用)
strcpy(str, "hello world");
printf(str);
}
修改一:
void GetMemory(char** p)//str的类型是char*,所以它的地址类型是char**
{
*p = (char*)malloc(100);//对p解引用就是对二级指针char**解引用,得到的是char*类型,即str
}
void Test(void)
{
char* str = NULL;
GetMemory(&str);//改为传址调用,p的改变才能影响str
strcpy(str, "hello world");
printf(str);
free(str);
str = NULL;
}
修改二:
//char* GetMemory(char* p)//这种参数只是为了创建p,没有太大意义
char* GetMemory()
{
//p = (char*)malloc(100);
char* p = (char*)malloc(100);
return p;
}
void Test(void)
{
char* str = NULL;
//str = GetMemory(str);//最好的方式是不需要传参,因为不需要传值过去
str = GetMemory();
strcpy(str, "hello world");
printf(str);
free(str);
str = NULL;
}
int main()
{
Test();
//打印方式补充
//printf("hello world\n");
//char* p = "hello world";//把字符串首字符h的地址赋给p
//printf(p);
return 0;
}
补充:nice笔试题
int* f1()
{
int x = 10;
return &x;
//出了函数x被销毁,所以&x地址指向的空间不属于x,造成野指针问题
}
int* f2()
{
int* ptr;
*ptr = 10;
return ptr;
//ptr没有初始化,里面是随机值,对ptr解引用相当于随便找了一块空间存放10,造成随机访问,即野指针问题
}
4.2 题目2:
char* GetMemory(void)
{
//返回栈空间地址问题
char p[] = "hello world";
//p数组是该函数的临时变量,出了函数p就被销毁,该空间还给了操作系统
//但是p还记录着该空间的地址
return p;
}
void Test(void)
{
char* str = NULL;
str = GetMemory();//str就是野指针
printf(str);
}
int main()
{
Test();
return 0;
}
4.3 题目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);
//没有释放,造成内存泄漏
//要释放str指针指向的空间,并且将str指针置为NULL
}
int main()
{
Test();
return 0;
}
4.4 题目4:
void Test(void)
{
char* str = (char*)malloc(100);
strcpy(str, "hello");
free(str);//free以后还要将str置为NULL
if (str != NULL)//此时str指向的空间已经被释放,但str还没变,里面还存着该空间的地址,造成野指针问题
{
strcpy(str, "world");
printf(str);
}
}
int main()
{
Test();
return 0;
}
第五章:C/C++程序的内存开辟
内核空间:用来跑操作系统的
栈:存放局部变量
堆:动态内存函数开辟的空间
数据段(静态区):储存全局数据、静态数据。比如全局变量和静态变量
代码段:【可执行代码】指把代码编译之后产生可执行程序的二进制指令;常量字符串
第六章:柔性数组
也许你从来没有听说过
柔性数组(
flexible array
)
这个概念,但是它确实是存在的。
C99
中,结构中的最后一个元素允许是未知大小的数组,这就叫做『柔性数组』成员。
typedef struct st_type
{
int i;
int a[0];//柔性数组成员,0表示不知道有几个元素
}type_a;
//有些编译器会报错无法编译可以改成下方形式:
typedef struct st_type
{
int i;
int a[];//柔性数组成员
}type_a;
6.1 柔性数组的特点
- 结构中的柔性数组成员前面必须至少一个其他成员。
- sizeof 返回的这种结构大小不包括柔性数组的内存。
- 包含柔性数组成员的结构用malloc ()函数进行内存的动态分配,并且分配的内存应该大于结构的大小,以适应柔性数组的预期大小。
struct S
{
int n;//结构中的柔性数组成员前面必须至少一个其他成员。
int arr[];//柔性数组成员
};
int main()
{
int sz = sizeof(struct S);
printf("%d\n", sz);//4;sizeof 返回的这种结构大小不包括柔性数组的内存。
//包含柔性数组的结构不能用下方的方式创建
//struct S s;//4byte 这种创建方式只有4个字节大小,没有给柔性数组开辟空间
//包含柔性数组成员的结构用malloc()函数进行内存的动态分配,并且分配的内存应该大于结构的大小,以适应柔性数组的预期大小。
return 0;
}
6.2 柔性数组的使用
struct S
{
int n;
int arr[];
};
int main()
{
struct S* ps = (struct S*)malloc(sizeof(struct S) + 40);
if (NULL == ps)
{
return 1;
}
//给成员赋值
ps->n = 100;
int i = 0;
for (i = 0; i < 10; i++)
{
ps->arr[i] = i;
}
//打印柔性数组
for (i = 0; i < 10; i++)
{
printf("%d ", ps->arr[i]);
}
//假设空间不够,继续开辟
struct S* ptr = (struct S*)realloc(ps, sizeof(struct S) + 80);
if (ptr != NULL)
{
ps = ptr;
ptr = NULL;
}
//释放
free(ps);
ps = NULL;
return 0;
}
使用指针模拟柔性数组
struct S
{
int n;
int* arr;
};
int main()
{
struct S* ps = (struct S*)malloc(sizeof(struct S));
if (NULL == ps)
return 1;
//给成员赋值
ps->n = 100;
ps->arr = (int*)malloc(40);//使用arr指针接收malloc开辟空间的地址
if (NULL == ps->arr)
return 1;
int i = 0;
for (i = 0; i < 10; i++)
{
ps->arr[i] = i;//等价于*(ps->arr + i)
}
//打印
for (i = 0; i < 10; i++)
{
printf("%d ", ps->arr[i]);
}
//扩容
int* ptr = (int*)realloc(ps->arr, 80);
if (NULL == ptr)
return 1;
//使用.....
//释放
free(ptr);
free(ps);
ps = NULL;
//这种方法使用了2次malloc和free,会造成内存碎片多或内存泄漏(忘记释放)
return 0;
}
6.3 柔性数组的优势
第一个好处是:
方便内存释放
如果我们的代码是在一个给别人用的函数中,你在里面做了二次内存分配,并把整个结构体返回给
用户。用户调用
free
可以释放结构体,但是用户并不知道这个结构体内的成员也需要
free
,所以你
不能指望用户来发现这个事。所以,如果我们把结构体的内存以及其成员要的内存一次性分配好
了,并返回给用户一个结构体指针,用户做一次
free
就可以把所有的内存也给释放掉。
第二个好处是:
这样有利于访问速度
.
连续的内存有益于提高访问速度,也有益于减少内存碎片。