Stack vs Heap. What's the difference and why should I care?
文章推薦指數: 80 %
We allocate memory from the heap using the malloc() function. The argument we want to include in malloc is the amount of memory we want to allocate to our ... GetunlimitedaccessOpeninappHomeNotificationsListsStoriesWriteStackvsHeap.What’sthedifferenceandwhyshouldIcare?I’mfourmonthsintotheselfstudyandI’vesolvedmultipleproblemsusingthemalloc,realloc,callocandfreefunctionsintheCprogramminglanguage.Whatbetterwaytobuildasolidfoundationofhowmemorygetsallocatedthentowriteatechnicalpostonthestackversustheheap?Thisarticleexplainsindepth:Whatarethefivesegmentsofmemory?Whatisthestack?Whatistheheap?Howdoesunderstandingthetwomakeyouabettersoftwareengineer?Whatarethefivesegmentsofmemory?Whenwewriteapplications,files,oranylogicthatistypedinaneditorandexecutedonthecomputer,thecomputerhastoallocatememoryfortheprogramtorun.Thememorythatisassignedtoaprogramorapplicationinacomputercanbedividedintofiveparts.Theamountofmemorythatget’sassignedtoanapplicationdependsonthecomputer’sarchitectureandwillvaryacrossmostdevices,butthevariablethatremainsconstantisthefivepartsofanapplication’smemorywhicharetheheap,stack,initializeddatasegment,uninitializeddatasegment,andthetextsegment.Theinitializeddatasegmentconsistsofalltheglobalandstaticvariablesthatareinitializedwhenafilegetscompiled.Theuninitializeddatasegmentconsistsofallglobalandstaticvariablesthatareinitializedtozeroordonothaveexplicitinitializationinsourcecode.MostofthetimeI’mnotconcernedabouttheuninitializeddatasegmentbecausewhenwecompileourprogramswithgcc,weusetheflags,-Wall-Wextra-pedantic-Werrorandweuseaninternalstylisticcheckercalledbettywhichtreatswarningaserrorswhenuninitializedvariablesarepresent.Havingunusedvariablesinourprogramsgetsflaggedandisnotabestpractice.Thetextsegment,alsoknownasthecodesegment,containsthemachineinstructionswhichmakeupyourprogram.Thetextsegmentisoftenread-onlyandpreventsaprogramfromaccidentallymodifyingitsinstructions.Whatisthestack?Thestackisasegmentofmemorywheredatalikeyourlocalvariablesandfunctioncallsgetaddedand/orremovedinalast-in-first-out(LIFO)manner.Whenyoucompileaprogram,thecompilerentersthroughthemainfunctionandastackframeiscreatedonthestack.Aframe,alsoknownasanactivationrecordisthecollectionofalldataonthestackassociatedwithonesubprogramcall.Themainfunctionandallthelocalvariablesarestoredinaninitialframe.ProgramvsStackusageInthepictureabove,wehaveonestackframeonthestackthatholdsthemainfunction,alongwiththelocala,bandsumvariables.Afterusingtheprintf()functiontheframewecreatedalongwiththelocalvariablesareonlyaccessibleinmemoryforthedurationoftheframearenolongeraccessibleafterreturningthe0valuefromthefunction.Whathappenswiththestackwhenwecallmultiplefunctions?Toillustratethestackinit’sLIFOmanner,let’ssolveaproblemusingrecursion.Whenwecallmultiplefunctionsinourapplication,weusemultiplestackframesinalast-in-first-outapproachmeaningthatthelaststackframewe’vecreatedonthestackisthefirststackthatwillbereleasedafterthefunctionisdoneexecutingitslogic.Let’sgooveranexampleofprintingoutthename“Holberton”recursivelyandshowhowourcodeaffectsthestackmemorysegment.Yes,Ihaveawhiteboardonthebackofmydooratmyhouse.Whenwecompileourcodeusinggcc_putchar.c0-puts_recursion.c0-main.c,thecompilerentersourprogramthroughintmain(void)andcreatesaframewiththefunctionintmain(void)and_puts_recursion("Holberton")livingonthatframeasillustratedontheimageabove.Whenthecompilerrunsintothe_puts_recursion()function,itcallsthatfunctionandcreatesanotherstackframeontopofthepreviousstackframewhereintmain(void)lives.Wearenowinoursecondstackframeinourprogramandhaveenteredinthe_puts_recursion(char*s)functionwhere*sisequalto'H'andisonlyaccessibleinthatstackframe.Because'H'doesnotequal'\0',wewillcontinuewithourfunctioncallsandexecutethe_putchar('H')functionandenterintothesamefunction_puts_recursion(++s).Theargument++smovesthememoryaddressofthe*sonebytebecausethesizeofacharis1byteonourmachine,andnow_puts_recursioniscallingthefunctionas_puts_recrusion('o').Eachtimethe_puts_recursionfunctioniscalled,anewstackframeisputonthestackuntilwehittheterminatingconditionwhichisif(*s=='\0').Everytimeanewstackframeiscreated,thestackpointermoveswithituntilitreachestheterminatingcondition.Astackpointerisasmallregisterthatstorestheaddressofthelastprogramrequestinaframe.Whenwehittheterminatingcondition,weexecuteourlogic,thenstarttounwindthestackorpopoffstackframesinthelast-in-first-outmanneruntilwereachoutreturn(0)logicintheintmain(void)functioninourfirststackframe.Ifyoudon’thaveaterminatingcasefortherecursiveexampleabove,thestackwillcontinuetogrowinsizeaddingadditionalstackframeson-topofeachother,movingthestackpointerupwardoneachcall,againsttheheap,whichwillbeexplainedinthenextsection.Inarecursivefunction,ifthereisnovalidterminatingcondition,thestackwillgrowuntilyou’vecompletedconsumedallthememorythat’sbeenallocatedforyourprogrambytheoperatingsystem.Whenthestackpointerexceedsthestackbound,youhaveaconditioncalledstackoverflow.Badthingshappenwhenyouhaveastackoverflow.Let’sfirstreferbacktotheotherfoursegmentsofyourapplication’smemorywhichweretheuninitializedandinitializeddatasegments,textsegmentandstacksegment.Thesefoursegmentshaveaconstantmemorysizeduringcompilation.Thememorysizeforthesefoursegmentsispredeterminedbyyouroperatingsystembeforecompilingyourprograms.Whensoftwareengineerswriteprogramsthatconsumelargeamountsofmemoryfromamachine,theyhavetoconsiderwhereandhowmuchmemoryisbeingconsumedintheirapplication.Themaxstacksizeisconstantandpredeterminedbeforeaprogramiscompiled.IuseaLinuxUbuntu/Trusty64distributions.Tofindinformationaboutthestacksizeandotherneatlimits,typethecommandbelowintoyourterminal.ulimit-aWhereulimitisafunctionthatgetsandsetsuserlimitsandthe-aflaglistsallthecurrentlimits.Stacksizeis8.192MBofmemory.Ifthestackislimitedinsizeandaprogramneedsmorememoryforittoexecute,wherecanasoftwareengineerpullmemoryfromforhis/herapplication?Thisiswheretheheapcomesintoplay.Whatistheheap?Theheapisthesegmentofmemorythatisnotsettoaconstantsizebeforecompilationandcanbecontrolleddynamicallybytheprogrammer.Thinkoftheheapasa“freepool”ofmemoryyoucanusewhenrunningyourapplication.ThesizeoftheheapforanapplicationisdeterminedbythephysicalconstraintsofyourRAM(Randomaccessmemory)andisgenerallymuchlargerinsizethanthestack.Weusememoryfromtheheapwhenwedon’tknowhowmuchspaceadatastructurewilltakeupinourprogram,whenweneedtoallocatemorememorythanwhat’savailableonthestack,orwhenweneedtocreatevariablesthatlastthedurationofourapplication.WecandothatintheCprogramminglanguagebyusingmalloc,realloc,callocand/orfree.Checkouttheexamplebelow.Allocating4000bytesofmemorytoourprogram,thenreleasingit.Weallocatememoryfromtheheapusingthemalloc()function.Theargumentwewanttoincludeinmallocistheamountofmemorywewanttoallocatetoourapplication,inbytes.Mallocreturnsavoidpointerthatistypecastedintoanintegerpointerthatnowpointstothefirstaddressinmemoryforour4000bytelongmemory.Wecannowstoreinformationinthosememoryaddressesanddoaswepleasetothatinformationforthedurationofourprogramorforthedurationofourfunctionbecausewehaveapointerthatreferencesthefirstmemoryaddressfromthenewlyallocatedheapmemory.Ifyouaren’tintentionallycreatingvariablesthatlastthedurationofyourapplicationfromtheheap,youalwayswanttoreleasethememorybacktothemachineusingthefree()function.Ifyoudon’treleasethememoryusingthefree()function,youhavememorythatwillpersistthroughoutyourprogram.Ifwedonotreleasethememoryfromourprogrambeforeterminatingtheapplication,ourapplicationhasmemoryleaks.Ifyourapplicationhasenoughmemoryleaks,itcanconsumemorememorythanisphysicallyavailableandcancauseprogramstocrash.Thisiswhyweuseaprogramcalledvalgrind.Valgrindiseasytouseandchecksformemoryleaks.Valgrindbeingused.4,000bytesallocated.0bytesleaksAnotherthingtoconsiderwhileusingtheheap,thepointervariablescreatedontheheapareaccessiblebyanyfunction,anywhereinyourprogram,aslongasthememoryisstillpersistentandhasn’tbeenfree.Howdoesunderstandingthestackandheapmakeyouabettersoftwareengineer?Ifyouunderstandtheadvantagesanddisadvantagesofusingthestackvstheheapforyourapplication,thenitgivesyouastrategicadvantageforcreatingscalableprograms.You,theprogrammer,havetodecidewhentousememoryfromthestackvsheapbasedoneachproblemyouaretryingtosolve.Ifyouhaveavariablelikeanarrayorstructthatneedstobestoredinalargeblockmemory,needstopersistthroughoutthelifetimeofyourapplicationandcouldchangeinsizethroughoutthedurationofyourprogram,thenyoushouldallocateitfromtheheap.Ifyouneedtocreatehelperfunctionswithvariablesthatonlypersistwithinthelifetimeofthefunction,thenyoushouldallocatememoryfromthestack.MemoryfromthestackiseasiertokeeptrackofbecausethememoryisonlylocallyavailableinthefunctioncallwhichdoesnotpersistafterthefunctioniscompletedandismanagedbytheCPU.Photocredit:GribbleLabQuestions,commentsorconcerns,feelfreetocommentbelow,followmeorfindmeonTwitter@NTTL_LTTN.References:MyCodeSchool.(February23rd,2013).Pointersanddynamicmemory—stackvsheap.[video].Retrievedfromhttps://www.youtube.com/watch?v=_8-ht2AKyH4PaulGribble(2012).CProgrammingBootCamp—7.Memory:StackvsHeap.[Blogpost].Retrievedfromhttps://www.gribblelab.org/CBootCamp/7_Memory_Stack_vs_Heap.html#orgheadline1GeeksforGeeks.MemoryLayoutofCPrograms.[Blogpost].Retrievedfromhttps://www.geeksforgeeks.org/memory-layout-of-c-program/SandraHenry-Stocker.(November18th,2012).NETWORKWORLD—Settinglimitswithulimit.[Blogpost].Retrievedfromhttps://www.networkworld.com/article/2693414/operating-systems/setting-limits-with-ulimit.htmlValgrindDevelopers(2000–2017).Valgrind.Retrievedfromhttp://valgrind.org/Die.netLinuxDocumentation.Retrievedfromhttps://linux.die.net/--5MorefromNickolasTeixeiraLanzaFollowSoftwareEngineerLovepodcastsoraudiobooks?Learnonthegowithournewapp.TryKnowableRecommendedfromMediumSteveHardingAddSoundstoYourRaspberryPiDeviceDeveduarStartwithManjaroKDEplasma,resourcesBrittanyGreenfieldinWabbiGettingStartedwithSecDevOps:TheWhat,TheHow,andtheWhySubashBasnetNumpyVectorizationSpeedTestwithRandomWalkExampleDennyBrandtinStartups.comStartupLessons:It’sTimetoImproveYourProductEngineeringConversationNetflixTechnologyBloginNetflixTechBlogEvolutionofApplicationDataCaching:FromRAMtoSSDDrewHarteveldTheIterativeInchwormAdamO'ReillyThebeautyofGoogleAnalyticsAboutHelpTermsPrivacyGettheMediumappGetstartedNickolasTeixeiraLanza157FollowersSoftwareEngineerFollowMorefromMediumAytaçKahveciDocumentingC++CodewithSphinxAlexanderFloydYou’reComparingFloatsWrongDaipayanBhowalWhyCisthemostpowerfullanguageAshleyGelwixMyFirstImpressionUsingSoftwareIdeasModelerUMLModelingToolforC++HelpStatusWritersBlogCareersPrivacyTermsAboutKnowable
延伸文章資訊
- 1memory allocation in Stack and Heap
- 2Dynamic Memory Allocation via malloc or the stack
malloc is the standard C way to allocate memory from "the heap", the area of memory where most of...
- 3C dynamic memory allocation - Wikipedia
Heap-basedEdit · dlmalloc and ptmallocEdit · FreeBSD's and NetBSD's jemallocEdit · OpenBSD's mall...
- 4Stack / Heap 區分-出自於藍森林(備份)
heap:是由malloc之類函數分配的空間所在地。地址是由低向高增長的。 stack:是自動分配變量,以及函數調用的時候所使用的一些空間。
- 5Stack vs Heap. What's the difference and why should I care?
We allocate memory from the heap using the malloc() function. The argument we want to include in ...