C++memorymanagementc++ Memory Management
Last Update:2018-07-18
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!