文章目录
- 🚀前言
- 🚀治理灵活内存的函数
-
- ✈️malloc函数
- ✈️free函数
- ✈️calloc函数
- ✈️realloc函数
- 🚀在经常使用灵活内存函数时的经常出现失误
-
- ✈️对NULL指针的解援用
- ✈️对灵活开拓空间的越界访问
- ✈️对非灵活开拓内存经常使用free监禁
- ✈️经常使用free监禁一块灵活开拓内存的一局部
- ✈️对同一块灵活内存屡次监禁
- ✈️灵活开拓内存遗记监禁(内存走漏)
铁子们好啊!当天阿辉给大家讲一下C言语外面特意关键的一块常识——灵活内存治理,为什么说它关键呢?置信大家在经常使用数组时都遇到过数字开拓空间太大或许太小的状况,这是很难堪的由于数组空间一旦确定就难以扩展或许增加。而当天的灵活内存治理将帮我们很好的处置这一困境。话不多说,开局我们当天的学习吧!!!
灵活内存的治理其实也就分为两块
- 空间的开拓和扩容
- 空间的监禁
而要做到以上两点,我们要引入几个关键的治理灵活内存的函数
灵活内存的开拓和监禁都是在堆区上
malloc
函数是用来开拓空间的,如何经常使用呢?上方是它的申明👇
voidmallocsize_tsize
关于
malloc
函数的留意事项:
-
size
示意malloc
函数所开拓空间的大小,单位是字节 -
maloc
函数关于开拓空间寄存何种类型的数据并不关心,前往值是void*
类型,malloc
开拓空间成功,则前往这块空间的起始位置的地址;若开拓空间失败,则前往空指针NULL
-
给
malloc
传入的参数为0
时,这种行为是C言语规范未定义的,取决于编译器 -
由
malloc
函数开拓的空间,外面的内容是随机的
详细经常使用:
intmianintpintmallocsizeofint10//开拓10*4个字节的空间,前往值强转为int*ifpNULL//判别灵活内存能否开拓成功printf"malloc开拓空间失败\n"//开拓失败前往调试语句,就是让程序员知道哪错了return1//开拓失败间接前往//对灵活开拓内存的进一步经常使用//这玩意其实就是个数组,有了首元素地址,不就是当成数组来玩嘛forinti0i10i//十个元素初始化成0~9pii//这玩意与*(p+i)=i等价,疑问得可以看阿辉之前关于指针的文章return0
关于
malloc
函数开拓的空间,它是一块延续的空间,间接当成数组经常使用一点故障没有,
它与数组最大的区别在于,
malloc
开拓的空间是在堆区上的,而数组是在栈区上的。
而堆区上放开的空间须有程序员自己手动监禁(Java,Python有渣滓回收机制),否则将形成内存走漏。其实就是你从我这拿了空间去用,不用了就还给我,别占着茅坑不拉屎,你不用他人也用不了。关于堆区放开的空间的监禁,这里要引入上方这个新的函数
C言语提供了一个函数free,专门用来做灵活内存的监禁和回收的,函数申明如下:
voidfreevoidptr
关于
free
函数经常使用时的留意事项:
-
关于传给
free
的参数必定是指向 灵活开拓内存 (也就是是堆区开拓的空间)的 起始地址的指针 -
若传给
free
的参数是空指针NULL
,free
函数则什么也不干 -
free
只会监禁传入的指针指向的空间,并不会给传入指针置空,这会很风险,空间你都还给操作系统了,你这个指针还指向那里,这个指针很野得治,所以free
完,指针我们要置空
intmian//开拓灵活空间intpintmallocsizeofint10//判别灵活内存能否开拓成功ifpNULLprintf"malloc开拓空间失败\n"return1/*对灵活开拓空间的经常使用*///不用啦,监禁空间freeppNULLreturn0
👆这段代码就是灵活内存的经常使用的基本形式,先开拓空间,而后判别能否开拓成功,接着经常使用这块空间,最后不用了把这块空间监禁掉
C言语中还提供了
calloc
函数,雷同是进执行态内存调配的,函数申明如下:
voidcallocsize_tnumsize_tsize
calloc
与
malloc
仅有两点不同
-
经常使用上
calloc
函数须要两个参数,开拓空间为num
个大小为size
个字节的空间 -
calloc
函数开拓的空间会把每个字节所有初始化为0
intmainintpintcalloc10sizeofintifpNULL//判别空间能否开拓成功printf"calloc开拓空间失败\n"return1forinti0i10i//打印灵活内存中的元素printf"%d"pifreep//空间监禁pNULL//指针置空return0
输入:
0000000000//十个0
铁子们预计有点不买账了,说好的灵活内存调配,讲了这么多也没有表现灵活俩字。铁子们别急,
realloc
函数才是当天的重头戏
C言语提供
realloc
函数用来使灵活内存的经常使用愈加灵敏,之前开拓的空间太大或许太小都可以灵敏的调整从新调配空间大小
realloc
函数的函数申明
voidreallocvoidptrsize_tsize
realloc
函数在经常使用时要留意上方这几点:
-
ptr
是要调整的空间的起始地址,ptr
得是灵活内存开拓的地址,若是ptr
为空指针,realloc
函数将与malloc
函数的成功一样 -
size
是realloc
函数调整大小后的内存空间大小 -
realloc
函数的前往值是空间调整后的起始地址,空间开拓失败前往空指针 -
realloc
函数扩容的空间外面的值并未被初始化,是随机值 -
这个函数在调整原内存空间大小的基础上,还会将原来内存中的数据移动到新的空间
-
关于
realloc
函数调整空间有了两种状况:-
状况一:原有空间后有足够空间
-
状况二:原有空间后无足够空间,原有空间将被监禁掉,从新找一块空间
-
例子:
intmainintpintmalloc5sizeofint//先用malloc放开5个整型大小的内存空间ifpNULL//判别能否开拓成功printf"malloc开拓空间失败\n"return1inti0fori0i5ipi1//把5个整型所有初始化为1//不够用了,要再参与5个整型intptrintreallocp10sizeofintifptrNULLprintf"realloc开拓空间失败\n"return1pptr//空间扩容成功将ptr的地址赋给pptrNULL//ptr置空fori0i10iprintf"%d"pifreeppNULLreturn0
上述,经常使用
realloc
对空间做调整时,必定要新创立一个变量来接纳
realloc
的前往值,由于假设经常使用指向原空间的起始地址的指针
p
来接纳,一旦
realloc
开拓空间失败,
p
将被赋值成
NULL
,将找不到原来空间形成内存走漏
voidtestintpintmallocINT_MAX4p20//假设p的值是NULL,就会有疑问freep
voidtestinti0intpintmalloc10sizeofintifNULLpexitEXIT_FAILUREfori0i10ipii//当i是10的时刻越界访问freep
voidtestinta10intpafreep//error——不能对非灵活内存启动监禁
voidtestintpintmalloc100pfreep//p不再指向灵活内存的起始位置
voidtestintpintmalloc100freepfreep//重复监禁
voidtestintpintmalloc100ifNULLpp20intmaintestwhile1
关于灵活开拓的内存必定要记得监禁,否则将形成内存走漏
假设感觉文章对你有协助的话,还请点赞,关注,收藏允许博主,如有无余还请领导,博主及时矫正,感谢大家允许!!!