C++memorymanagementc++ Memory Management

Source: Internet
Author: User
Problem C++hasseveraldistinctmemoryareaswhereobjectsandnon-objectvaluesmaybestored, Andeachareahasdifferentcharacteristics. Nameasmanyofthedistinctmemoryareasasyoucan.foreach, Summarizeitsperformancecharacteristicsanddescribethelifetimeofobjectsstoredthere. Example:thestackstoresautomaticvariables,includingbothbuiltinsandobjectsofclasstype. Solution Thefollowingsummarizesac++program ' Smajordistinctmemoryareas. Notethatsomeofthenames (e.g, "heap") Donotappearassuchinthedraft [Standard]. Memoryareacharacteristicsandobjectlifetimes-------------------------------------------------------------- Constdatatheconstdataareastoresstringliteralsand otherdatawhosevaluesareknownatcompile time. Noobjectsofclasstypecanexistin Thisarea. Alldatainthisareaisavailable Duringtheentirelifetimeoftheprogram. Further,allofthisdataisread-only,andthe resultsoftryingtomodifyitareundefined. Thisisinpartbecauseeventheunderlying storageformatissubjecttoarbitrary optimizationbytheimplementation. For Example,aparticularcompilermaystorestring Literalsinoverlappingobjectsifitwantsto. stackthestackstoresautomaticvariables.typically allocationismuchfasterthanfordynamic Storage (Heaporfreestore) Becauseamemory allocationinvolvesonlypointerincrement ratherthanmorecomplexmanagement. Objects Areconstructedimmediatelyaftermemoryis Allocatedanddestroyedimmediatelybefore memoryisdeallocated, Sothereisno opportunityforprogrammerstodirectly Manipulateallocatedbutuninitializedstack Space ( BarringwillfultamperingusingeXplicit dtorsandplacementnew). Freestorethefreestoreisoneofthetwodynamicmemory Areas,allocated/freedbynew/delete. Object Lifetimecanbelessthanthetimethestorage Isallocated;thatis,freestoreobjectscan Havememoryallocatedwithoutbeingimmediately initialized,andcanbedestroyedwithoutthe Memorybeingimmediatelydeallocated. During theperiodwhenthestorageisallocatedbut outsidetheobject ' Slifetime,thestoragemay Beaccessedandmanipulatedthroughavoid*but noneoftheproto-object ' Snonstaticmembersor memberfunctionsmaybeaccessed, Havetheir addressestaken,orbeotherwisemanipulated. Heaptheheapistheotherdynamicmemoryarea, Allocated/freedbymalloc/freeandtheir variants. Notethatwhilethedefaultglobal newanddeletemightbeimplementedintermsof mallocandfreebyaparticularcompiler,the Heapisnotthesameasfreestoreandmemory allocatedinoneareacannotbesafely Deallocatedintheother. Memoryallocatedfrom Theheapcanbeusedforobjectsofclasstype byplacement-newconstructionandexplicit destruction. Ifsoused,thenotesaboutfree StoreobjectliFetimeapplysimilarlyhere. Global/staticglobalorstaticvariablesandobjectshave theirstorageallocatedatprogramstartup,but Maynotbeinitializeduntilaftertheprogram hasbegunexecuting. Forinstance,astatic variableinafunctionisinitializedonlythe firsttimeprogramexecutionpassesthroughits definition. Theorderofinitializationof Globalvariablesacrosstranslationunitsisnot Defined,andspecialcareisneededtomanage Dependenciesbetweenglobalobjects (including classstatics). Asalways,uninitializedproto-objects ' storagemaybeaccessedandmanipulated throughavoid*butnononstaticmembersor memberfunctionsmaybeusedorreferenced Outsidetheobject ' Sactuallifetime. NoteaboutHeapvs.FreeStore:Wedistinguishbetween "Heap" and "Freestore" Becausethedraftdeliberatelyleavesunspecifiedthequestionofwhetherthesetwoareasarerelated. Forexample,whenmemoryisdeallocatedviaoperatordelete,18.4.1.1states: Itisunspecifiedunderwhatconditionspartorallofsuchreclaimedstorageisallocatedbyasubsequentcalltooperatorneworanyofcall Oc,malloc,orrealloc,declaredin<cstdlib>. Similarly,itisunspecifiedwhethernew/deleteisimplementedintermsofmalloc/freeorviceversainagivenimplementation. effectively,thetwoareasbehavedifferentlyandareaccesseddifferently--sobesuretousethemdifferently!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.