建立网站的方案,南京小程序开发网站建设,全国室内设计公司排名,企业网站后台模板动态内存管理详解 前言#xff1a;一、为什么存在动态内存分配二、动态内存函数2.1malloc函数2.2calloc函数2.3realloc函数2.4free函数 三、常见的动态内存错误3.1 对NULL指针解引用操作3.2 对动态开辟空间的越界访问3.3 对非动态开辟内存使用free释放3.4 使用free释放动态开辟… 动态内存管理详解 前言一、为什么存在动态内存分配二、动态内存函数2.1malloc函数2.2calloc函数2.3realloc函数2.4free函数 三、常见的动态内存错误3.1 对NULL指针解引用操作3.2 对动态开辟空间的越界访问3.3 对非动态开辟内存使用free释放3.4 使用free释放动态开辟内存的一部分3.5 对同一块内存多次释放3.6 动态开辟内存忘记释放内存泄漏 四、几个经典的例子五、c/c程序的内存分配六、柔性数组6.1柔型数组的特点6.2 柔性数组的使用6.3柔性数组的优势 前言
我们一般开辟内存是直接开辟空间开辟了空间就不会改变了为了更节约空间避免浪费空间我们可以动态的开辟空间。这样空间用完了我们可以扩充空间。
一、为什么存在动态内存分配
我们已经掌握的内存开辟方式 int vai10 / /在栈空间上开辟四个字节。 int arr[10]{0}; / /在栈空间上开辟40个字节的连续空间。 特点 空间开辟大小是固定的。数组在申明的时候必须指定长度才能在编译的时候分配空间。 对于空间的需求不仅仅是上述的情况有时候我们需要的空间大小只有在程序运行的时候才会知道数组在编译时开辟空间的方式就不能满足了。
二、动态内存函数 关于动态内存函数的知识可以参考我的另一篇文章动态内存函数详解 2.1malloc函数 void* mallocsize_t size; malloc函数向内存申请一块连续可用的空间返回指向这个空间的指针。 malloc函数申请到空间后直接返回这块空间的起始地址不会初始化空间的内容。 malloc申请的空间当程序退出时还给操作系统当程序不退出动态申请的内存不会主动释放需要用free函数来释放
2.2calloc函数 void* calloc(size_t num , size_t size) ; calloc函数也可以申请动态内存空间。并且给空间初始化为0。
2.3realloc函数 void* realloc( void* ptr , size_t size); realloc函数可以申请动态内存空间使动态内存空间管理更灵活。
2.4free函数 void* free (void* ptr ) ; free函数只能释放动态开辟的内存。
三、常见的动态内存错误
3.1 对NULL指针解引用操作
可能运行成功
#include stdio.h
#include stdlib.h
void test()
{int* p (int*)malloc(10 * sizeof(int));*p 20;//如果p的值是NULL就会有问题free(p);
}
int main()
{test();return 0;
}解决方法需要对空指针进行判定空指针不能被赋值就是p为空不能解引用
#include stdio.h
#include stdlib.h
void test()
{int* p (int*)malloc(10 * sizeof(int));if (p NULL){perror(malloc);return;}*p 20;//如果p的值是NULL就会有问题free(p);
}
int main()
{test();return 0;
}3.2 对动态开辟空间的越界访问
越界访问系统会崩溃
#include stdio.h
#include stdlib.h
void test()
{int i 0;int* p (int*)malloc(10 * sizeof(int));if (p NULL){perror(malloc);return;}for (i 0; i 10; i){*(p i) i 1;//当i10时越界访问}free(p);
}
int main()
{test();return 0;
}解决因为要访问的空间存在越界问题那么我们就让它不越界访问
#include stdio.h
#include stdlib.h
void test()
{int i 0;int* p (int*)malloc(10 * sizeof(int));if (p NULL){perror(malloc);return;}for (i 0; i 10; i)//改为i 10{*(p i) i 1;}free(p);
}
int main()
{test();return 0;
}3.3 对非动态开辟内存使用free释放
程序崩溃
#include stdio.h
#include stdlib.h
void test()
{int a 10;int* p a;//释放非动态开辟内存空间有问题free(p);//不行运行程序系统崩溃
}
int main()
{test();return 0;
}解决方法非动态开辟的内存不要用free函数释放
3.4 使用free释放动态开辟内存的一部分
系统崩溃
#include stdio.h
#include stdlib.h
void test()
{int* p (int*)malloc(10 * sizeof(int));if (p NULL){perror(malloc);return;}int i 0;for (i 0; i 5; i){*p i;p;}//释放free(p);//p不在指向动态内存的起始位置p NULL;
}
int main()
{test();return 0;
}解决问题动态开辟内存起始位置不能乱使用释放的时候要从起始位置全部释放开辟的一块连续的空间 p这里p不是指向开辟空间的起始位置释放的是后面的一部分前面没有释放程序崩溃了。 3.5 对同一块内存多次释放
程序崩溃了不允许这么操作。
#include stdio.h
#include stdlib.h
void test()
{int* p (int*)malloc(10 * sizeof(int));if (p NULL){perror(malloc);return;}//使用//释放free(p);//释放free(p);//重复释放程序就崩了
}
int main()
{test();return 0;
}解决方法第一次释放结束把p置NULL下一次释放释放空指针啥事都不干。
#include stdio.h
#include stdlib.h
void test()
{int* p (int*)malloc(10 * sizeof(int));if (p NULL){perror(malloc);return;}//使用//释放free(p);//释放结束把p置空p NULL;//释放free(p);
}
int main()
{test();return 0;
}3.6 动态开辟内存忘记释放内存泄漏
找不到p没有释放技术内存泄漏
#include stdio.h
#include stdlib.h
void test()
{int* p (int*)malloc(100);if (p NULL){perror(malloc);return;}*p 20;
}
int main()
{test();//回来之后p被销毁while (1);return 0;
}把100个字节空间的起始地址赋值给p。p是函数里的局部变量。出了这个函数p被销毁找不到这100个字节的空间了但是这100个字节的空间还在。
解决方法只有两种方式可以销毁。
free函数释放。程序结束退出
注意 动态申请的内存空间不会因为出了作用域就自动销毁也·不会把内存还给操作系统。
四、几个经典的例子
题目1: 存在两个问题
#include stdio.h
#include stdlib.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;
}题目2 存在一个问题
#include stdio.h
#include string.h
char* Getmemory()
{char p[] hello world;//函数结束内存释放空间还给操作系统。
}
void test(void)
{char* str NULL;strGetmemory();//记住了地址却找不到数据这是个野指针它非法访问strcpy(str, hello world);printf(str);
}
int main()
{test();return 0;
}题目3 存在一个问题
#include stdio.h
#include string.h
#include stdlib.h
void Getmemory(char** p, int num)
{*p (char*)malloc(num);
}
void test(void)
{char* str NULL;Getmemory(str,100);strcpy(str, hello world);printf(str);//内存泄漏//free(str);//strNULL;
}
int main()
{test();return 0;
}题目4 存在一个问题
#include stdio.h
#include string.h
#include stdlib.hvoid test(void)
{char* str (char*) malloc(100);strcpy(str, hello);free(str);//内存被释放str成为野指针。if (str ! NULL)//肯定成立{strcpy(str, world);//非法访问printf(str);}printf(str);
}
int main()
{test();return 0;
}五、c/c程序的内存分配
初步了解c/c中内存区域的划分 1.栈区(stack) 在执行函数时 函数内局部变量的存储单元都可以在栈上创建函数执行结束时这些存储单元自动被释放。栈内存分配运算内置于处理器的指令集中效率很高但是分配的内存容量有限。栈区主要存放运行函数而分配的局部变量、 函数参数、返回数据、返回地址等。 2.堆区(heap) 一般由程序员分配释放 若程序员不释放 程序结束时可能由OS回收分配方式类似于链表 3.数据段(静态区) (static) 存放全局变量静态数据。 程序结束后由系统释放。 4.代码段存放函数体(类成员函数和全局函数)的二进制代码。 注意 实际上普通的局部变量是在栈区分配空间的栈区的特点是在上面创建的变量出了作用域就销毁。但是被static修饰的变量存放在数据段(静态区)数据段的特点是在上面创建的变量直到程序结束才销毁。 所以static修饰的变量生命周期变长了。
六、柔性数组
结构体中最后一个成员是数组或者是指针他们是有区别的。 c99中结构体的最后一个成员可以是未知大小的数组这叫柔性数组的成员。 结构体
typedef struct st
{int i;int arr[];
}ts;6.1柔型数组的特点
结构体中柔性数组前面必须至少有一个其他成员。 sizeof返回的这种结构的大小不包括柔性数组的内存。只计算柔性数组前面成员的大小 包含柔性数组成员的结构用malloc函数进行内存的动态分配并且分配的内存应该大于结构的大小以适应柔性数组的预期大小。 如
ts* ps (ts*)malloc(sizeof(ts) 40);6.2 柔性数组的使用
int main()
{
int i 0;
ts* p (ts*)malloc(sizeof(ts) 100 * sizeof(int));
if (p NULL)
{perror(malloc);return 0 ;
}p-i 100;
for (i 0; i 100; i)
{p-arr[i] i 1;
}
free(p);
p NULL;
return 0;
}柔性数组成员a相当于获得了100个整型元素的连续空间
6.3柔性数组的优势
代码1
int i 0;
ts* p (ts*)malloc(sizeof(ts) 100 * sizeof(int));
if (p NULL)
{perror(malloc);return 0 ;
}p-i 100;
for (i 0; i 100; i)
{p-arr[i] i 1;
}
free(p);
p NULL;代码2
typedef struct st
{int i;int* p;
}ts;
ts* ps (ts*)malloc(sizeof(ts));
ps-i 100;
ps-p (int*)malloc(ps-i * sizeof(int));
for (i 0; i 100; i)
{ps-p[i] i 1;
}
//释放空间
free(ps-p);//先释放里面
ps-p NULL;
free(ps);//后释放外面
ps NULL;上面代码1和代码2可以完成相同的功能但是代码1的实现有两种好处。 第一个好处方便内存释放 如果我们的代码是在一个给别人用的函数中 你在里面做了二次内存分配并把整个结构体返回给用户。用户调用free可以释放结构体但是用户并不知道这个结构体内的成员也需要free,所以你不能指望用户来发现这个事。所以如果我们把结构体的内存以及其成员要的内存一次性分配好了并返回给用户一个结构体指针用户做一次free就可以把所有的内存也给释放掉。 第二个好处:这样有利于访问速度 连续的内存有益于提高访问速度也有益于减少内存碎片。