Memory management in C programsWord格式.docx

上传人:b****3 文档编号:15786815 上传时间:2022-11-16 格式:DOCX 页数:20 大小:37.48KB
下载 相关 举报
Memory management in C programsWord格式.docx_第1页
第1页 / 共20页
Memory management in C programsWord格式.docx_第2页
第2页 / 共20页
Memory management in C programsWord格式.docx_第3页
第3页 / 共20页
Memory management in C programsWord格式.docx_第4页
第4页 / 共20页
Memory management in C programsWord格式.docx_第5页
第5页 / 共20页
点击查看更多>>
下载资源
资源描述

Memory management in C programsWord格式.docx

《Memory management in C programsWord格式.docx》由会员分享,可在线阅读,更多相关《Memory management in C programsWord格式.docx(20页珍藏版)》请在冰豆网上搜索。

Memory management in C programsWord格式.docx

ThereareseveraltechniquesavailableformemorymanagementinC.ManyofthemareusedinNetHack3.4.3;

andevenmoreareusedsomewhereinNetHack4.Inthisblogpost,I'

dliketolookatthemanddiscusstheiradvantagesanddisadvantages.I'

mmostlyconcernedaboutcorrectness,ratherthanefficiency,here;

thatmeansthatunlesstheperformancedifferenceisverylarge,IcaremoreaboutcleancodethanIdoaboutfastcode.

Techniquesforallocatingfixedamountsofmemory

Therearebasicallytwodifferentissueshere:

trackingthelifetimeofmemoryallocations,andensuringthatthey'

retherightsize.Assuch,I'

llstartbylookingatallocationswhosesizeisstaticallyknownatcompiletime,andthenmoveontotechniquesthatcanhandlethesizebeingunknown.

Fixed-sizebuffersonthestack

ProbablythesimplestwaytoallocatememoryinCistouseastackallocation;

ifavariableisdeclaredinsideafunctionwithoutspecifyingstaticorextern,enoughmemoryforthatvariablewillbeallocatedwhenthatvariable'

sscopestarts,andfreedagainwhenthatvariable'

sscopeends(sometimesknownasan"

automaticvariable"

).BecausescopesinCarewell-nested,atypicalCimplementationwillimplementthisviausingastackofmemory;

newlyallocatedvariableswillbegivenspacefromthe"

top"

ofthestack,andthatspacewillbereleasedfromthetopofthestackwhentheyleavescope.("

Top"

isinquotesbecauseit'

sfarfromrareforastacktobeupside-downinC,withthetopbeingtheendwiththelowestmemoryaddress.)

Therearesomemajoradvantages(andsomeminoradvantages)ofdoingthingsthisway:

∙Itdoesnotrequireanyextrastate,beyondthatprovidedbytheimplementation,towork.Quiteoften,memoryallocationschemeswillthemselvesneedmemorytowork,leadingtosomethingofaninfiniteregress.Stackallocationsareoftenusefultoprovidethatmemory.

∙Unlikethevastmajorityofmemoryallocationschemes,itisexception-safewithnoextraeffort:

longjmpisawareofstackallocations,andwillautomaticallyfreethemfromanyscopeitjumpsoutof.(Somecareisneededinthefunctioncontainingsetjmpitself;

anymemorythatneedstopersistpastalongjmpmustbemarkedasvolatile,arequirementsetbythestandardinordertoallowcompilerstooptimizearoundlongjmpcallsmoreeasily.Technicallyspeaking,thememorydoesnotneedtobemarkedasvolatileifitsvaluedoesnotchangebewtweensetjmpandthematchinglongjmp;

inpractice,althoughcompilerscanhandlethiscase,theytendtospewwarnings,soitmakessensetomarkthememoryasvolatileifitisreadafterthelongjmpatall.)

ThismostlydoesnotmatterinNetHack3.4.3,whichdoesnotuseanyexception-likebehaviour(ifsomethinggoeswrong,itcallspanicwhichattemptstoproduceasavefile,andthenexitstheentireprocess,freeingallthememorythatway).However,NetHack4usesexception-likebehaviourinternally(implementedusinglongjmpandsetjmp,becauseCdoesnothavetrueexceptions);

NitroHack(andthusNetHack4)usesexceptionsinordertoexitagamewithoutexitingtheprocess.FromNetHack4.3(thecurrentdevelopmentversion,whichisunreleasedbutavailableusingthesavebreakbranchoftherepository)onwards,theyareusedevenmoreheavily,handlingsituationslikerewindingthegamemid-turn.

∙Itworkscorrectlyinthepresenceofmultithreading,recursion,andasynchronoussignals.

∙ThistechniquedoesnotrequireanyglobalstateapartfromthatmanagedbytheCcompiler;

thus,itisimpossibleto,forinstance,forgettosaveavariablestoredinastackallocation(unlessthesavecodeisrunninginsideascopecontainingit),ortohaveitaccidentallypersistfromonegameintothenext.

∙Ifyourcodedoesendupoverflowingafixed-sizedbuffer,yourprogramisgoingtobeincorrectnomatterwhat,butifthatbufferisallocatedonthestack,itmaximizesthechancethatyourcompilerwillbeabletogeneratecodetocatchthebufferoverflow(especiallyiftheoverflowhappensinacalltoastandardstringmanipulationfunctionlikesprintforstrcat).Itisincrediblyunwisetorelyonthistoprotectyourcodefromexploits,butit'

saratherhelpfulfeaturefordebugging.(Staticallyallocatedmemoryisalmostasgoodinthisrespect,butdynamicallocationsarehopeless.)

Itisthusnosurprisethatstackalloc

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 幼儿教育 > 育儿知识

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1