欢迎来到天天文库
浏览记录
ID:41293508
大小:281.81 KB
页数:21页
时间:2019-08-21
《DynamicMemoryAllocationppt动态内存allocationppt》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、DynamicMemoryAllocationAllofthecodewehavewrittenuptonowallocatesspacefordataatcompiletime.Wespecifythevariablesandthearraysizesthatweneedinthesourcecode,andthat’swhatwillbeallocatedwhentheprogramexecutes,whetherweneeditornot.Workingwithafixedsetofvariablesinaprogramcanbeveryrestrictive,anditi
2、softenwasteful.DynamicMemoryAllocation1tMynThesolutionistousedynamicmemoryallocation,whichmeansthatyouallocatethememoryyouneedtostorethedatayouareworkingwithwhenyourprogramexecutes(runtime),ratherthanwhenyoucompileit(compiletime).Dynamicallyallocatedvariablescan’tbedeclaredatcompiletime,andso
3、theycan’tbenamedinyoursourceprogram.Whenyouallocatememorydynamically,thespacethatismadeavailablebyyourrequestisidentifiedbyitsaddress.Theobviousplacetostorethisaddressisinapointer.DynamicMemoryAllocation2tMynMemoryAllocatedStaticallyMemoryAllocatedDynamicallyFixedDataAreaInflexibleWastefulFix
4、edcapacityProgramCodeProgramCodeFixedDataAreaDataareaallocateddynamicallyatruntimeFlexibleEfficientVariablecapacityDynamicMemoryAllocation3tMynInmostinstances,thereisunusedmemoryinyourcomputerwhenyourprogramisexecuted.InC++,thisunusedmemoryiscalledthefreestore,orsometimestheheap.Youcanallocat
5、espacewithinthefreestoreforanewvariableofagiventypebyusingaspecialC++operatorthatreturnstheaddressofthespaceallocated.Thisoperatorisnew,anditiscomplementedbytheoperatordelete,whichde-allocatesmemorythatyouhavepreviouslyallocatedwithnew.DynamicMemoryAllocation4tMynYoucanallocatespaceinthefrees
6、toreforsomevariablesinonepartofaprogram,andthenreleasetheallocatedspaceandreturnittothefreestoreonceyouhavefinishedwiththevariablesconcerned.Thememorythenbecomesavailableforreusebyotherdynamicallyallocatedvariableslaterinthesameprogram.Whenyouallocatespaceforavariableusingnew,youarecreatingth
7、evariableinthefreestore.Thevariablecontinuestoexistuntilthememoryitoccupiesisreleasedbytheoperatordelete.DynamicMemoryAllocation5tMynSupposethatweneedspaceforavariableoftypedouble.Wecandefineapointertotypedouble,andthenrequestthatthememoryisa
此文档下载收益归作者所有