资源描述:
《动态指针数组,释放内存问题(dynamic pointer array, freeing memory problems)》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、动态指针数组,释放内存问题(Dynamicpointerarray,freeingmemoryproblems)Clanguagememoryallocationandrelease(constructionofmultidimensionaldynamicarrays)(2012-02-2900:17)Tags:Clanguagememorydynamicclassification:C/C++Astaticanddynamicarrayarray.Staticarrayismorecommon,thelengthofthearrayofpredefi
2、ned,throughouttheprogram,onceagivensizecannotbechangedafterthelengthofstaticmemoryarraythemselvesresponsibleforthereleaseofoccupation.Dynamicarraylengthcanbewiththeprogramneedstospecifythesize.Thearrayofdynamicmemoryallocationfunction(malloc)fromthestack(heap)ontheallocationofsto
3、ragespace,onlywhentheprogramexecutionallocationfunction,toallocatememoryforitatthesametime,theprogrammerisresponsibleforreleasingtheallocatedmemory(free).Two.Whydoyouwanttouseadynamicarray?Inpractice,oftenthishappens,therequiredmemoryspacedependsontheactualinputdata,andcannotbede
4、terminedinadvance.Forthisproblem,itisdifficulttosolvebystaticarraymethod.Inordertosolvetheaboveproblems,theClanguageprovidessomememorymanagementfunctions,thesememorymanagementfunctionswiththepointercanbedynamicallyallocatedmemoryspace,tobuildadynamicarray,butalsothespaceforrecove
5、ryisnolongerinuse,providesameansfortheeffectiveuseofmemoryresources.Three.ComparedwiththestaticdynamicarrayarrayForstaticarray,itscreationisveryconvenient,thereisnoneedtouseaftertherelease,butalsocreatereferencestothesimple,unabletochangeitssizeisitsfatalweakness!Foradynamicarray
6、,itcreatestrouble,afterusemustbereleasedbytheprogrammersthemselves,otherwisewillcauseseriousmemoryleaks.Butitsuseisveryflexible,accordingtotheproceduresneedtobedynamicallyallocatedsize.Four.Howtobuildadynamicarray?Constructionofdynamicarray,wefollowthefollowingprinciples:Whenthea
7、pplicationfromoutertoinnerlayer,applicationlayer;Whenreleasedfromtheinnerlayertotheouterlayer,release;Five.ToconstructadynamicarraytopointerFortheconstructionofaone-dimensionaldynamicarray,pointertoonedimension;Forthetwo-dimensional,requiresaone-dimensional,two-dimensionalpointer
8、;For3D,needone,two,threepointer;Byanalog