Stack vs Heap. What's the difference and why should I care?
文章推薦指數: 80 %
I'm four months into the self study and I've solved multiple problems using the malloc, realloc, calloc and free functions in the C programming language. 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/MorefromNickolasTeixeiraLanzaFollowSoftwareEngineerLovepodcastsoraudiobooks?Learnonthegowithournewapp.TryKnowableRecommendedfromMediumAnuragSrivastavainFAUNPublicationAnalyzeyourprojectwithSonarQubeCoryAdamsBootCamp10 ….sofarimdadulhuqeMonetization:HowtodevelopuserPersonatoenablesaneffectivemonetizationstrategyVonageDevincodeburstHowtoSendanSMSWithBlazorDarioDeSantisinJavarevisitedBestoftheWeek — September13/20HjaafarEveryonewhodoubtsyouwillalwayscomebackaroundZackFrankhas_many:problems(butarelationain’tone)Naveen-(Founder&Trainer@NPNTraining)FlightdelaydatasetAnalysisusingHiveAboutHelpTermsPrivacyGettheMediumappGetstartedNickolasTeixeiraLanza158FollowersSoftwareEngineerFollowMorefromMediumManishChandraIndividualcontributorstoLinuxprojectishigherthananyindividualcompanyNamanTamrakarAsimpleprogramtodetectmemoryleaksinourCprogramcleofasProgramminganArduinoinLinuxTinaHuynhHowtoDevelopLinuxApplications(Part2)HelpStatusWritersBlogCareersPrivacyTermsAboutKnowable
延伸文章資訊
- 1Pointers, Stack & Heap Memory, malloc( )
The malloc( ) (memory allocate) function can be used to dynamically allocate an area of memory to...
- 2C dynamic memory allocation - Wikipedia
Hoard malloc
- 3Dynamic 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...
- 4Virtual Memory - Heap and Stack Memory - Course Websites
- 5Stack vs Heap. What's the difference and why should I care?
I'm four months into the self study and I've solved multiple problems using the malloc, realloc, ...