ssd6复习PPT_lecture3

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

Lecture3CProgrammingModelTheContentscover:3.1TheWonderofProgramExecution3.2TheVisualC++Debugger3.3VariablesandAddresses3.4DataandFunctionCalls3.5TheCodeItselfLecture3CProgrammingModelTheWonderofProgramExecution•Programmingisadifficulttaskrequiringastrictexerciseoflogicandacapacitytoorganizesubstantialamountsofinformation--MIRACULOUS!!!•Programsendupcommandinginnumerabledevicesandmechanismsincomplicatedsequencesoftinyevents•Theseevents,whichappearinsignificantwhenconsideredindividually,behaveaccordingtothespecificationofourprogramswhenconsideredtogether–CEO!Lecture3CProgrammingModelTheWonderofProgramExecution•Processi.e.c=a+b•LevelsofAbstractionoclassicthinkingophysicalissuesLecture3CProgrammingModelTheWonderofProgramExecution•LevelsofAbstractionoCprogrammingmodelisitselfanabstractionoOnecannamevariableswithmnemonicnames,insteadofhavingtospecifywherethevariablesarestoredoOnecanwritearray[i],insteadofhavingtocomputethelocationoftheithelementofarrayyourselfoOnecanwritec=a+binsteadofhavingtogivetheCPUspecificinstructionsonhowtocarryouttheaddition.•CcompilertakescareoftranslatingCprogramsintomachinecode.Lecture3CProgrammingModelunixgcc–ohellohello.cTheWonderofProgramExecutionLecture3CProgrammingModelTheWonderofProgramExecution•ExecutionasPhysicalProcess•Wethinkoofvariablesanddatatypesratherthanofmemorychips;oofalgorithmsratherthanofmovingdataamongthosechips;oofprogramstatementsratherthanofwhereandhowthesestatementsarestored.•Wedon'tgivemuchthoughttoexecutionprocessbecausewedon'thavetoLecture3CProgrammingModelTheWonderofProgramExecution•Onereasontodistinguishtheprogramfromitsexecutionisthatthetranslationisnotquiteperfect–NOTexpectedresults•Forinstance:oOutofmemoryopointers•CompilersandDebuggers•TheVisualC++EnvironmentLecture3CProgrammingModelSomemisunderstandingsforCprogrammers•Focusedonlogicmodelsinsteadofprogrammingmodels•Memoryisinfinite•Donotcareaboutdeploymentofmemories•Ignorephysicalprocessofprograms•Surprisedon:oa+b(a+b)oPointersmisleadingtowrongwayoThisisnotwhatIwant!Lecture3CProgrammingModelSomemisunderstandingsforCprogrammersLecture3CProgrammingModel#defineARRAY_SIZE10voidnatural_numbers(void){inti;intarray[ARRAY_SIZE];i=1;while(i=ARRAY_SIZE){array[i]=i-1;i=i+1;}}SomemisunderstandingsforCprogrammersLecture3CProgrammingModelSeealso:Unit3.MemoryLayoutandAllocationTheVisualC++Debugger•CreatingaCProgramUsingVisualC++•BreakpointsandSteps•ExaminingData•ExampleLecture3CProgrammingModelTheVisualC++DebuggerLecture3CProgrammingModelTheVisualC++DebuggerLecture3CProgrammingModelTheVisual.NETC++EnvironmentLecture3CProgrammingModelTheVisual.NETC++EnvironmentLecture3CProgrammingModelTheVisualC++Debugger•Anexample:See:1.2.4“Example”Lecture3CProgrammingModelVariablesandAddresses•AddressesandNaming•Pointers:StoredAddresses(Read)•ExaminingMemory(Read)•ArraysandStrings(Read)•NaughtyPointersLecture3CProgrammingModelAddressesandNaming•Inhardware,alldataisstoredinmemory•Memoryisasequenceofbytesthatisnumbered,startingwith0•ThemachinecodethattheCPUexecutesoperatesonmemorylocations,identifiedonlybytheirAddresses•Compilerstakecareoftranslatingtheoperationsourprogramsperformonvariablesintooperationsperformedonaddresses•NeitherthenamenorthetypeofthevariablessurvivesthistranslationLecture3CProgrammingModelAddressesandNaming•Forthemostpart,programmersdon'tcareabouttheaddressesdealingwiththeirvariables•Forexample,Javaensurethattheprogrammerlivesbyitsrules•ButCgivestheprogrammerthefreedomtodojustaboutanything•Withfreedomcomesresponsibility•CletsprogrammersshootthemselvesinthefootbywritingcodethatsubvertsthecleanabstractionsprogramminglanguagesusuallyprovideLecture3CProgrammingModelAddressesandNaming•&and*unaryoperators•The&operatorreturnstheaddressinwhichavariableorexpressionisstored•The*operatordoesthereverse:itreturnsthevaluestoredintheaddressgiventoit•Thisleadstricky:aCprogrammercanmaketwovariablenamesrefertothesamelocationinmemory••thehardwarecannotdistinguish•AndtheabovewillnotbeapparentinthesourcecodeLecture3CProgrammingModelVariablesandAddresses•AddressesandNaming•#includestdio.h•charglobalchar1;•charglobalchar2='g';•intglobalint1;•intglobalint2=9;•charglobalchar3;•intmain(intargc,char*argv[])•{•charlocalchar1;•charlocalchar2='l';;•intlocalint1;•intlocalint2=1;•charlocalchar3;•......•}Lecture3CProgrammingModelVariablesandAddresses•AddressesandNamingLecture3CProgrammingModelVariablesandAddressesLecture3CProgrammingModelProgramcodeanddata.Codebeginsatthesamefixedaddress,followedbydatalocationsthatcorrespondtoglobalCvariables.Heap.Thecodeanddataareasarefollowedimmediatelybytherun-timeheap.Sharedlibraries.NearthemiddleoftheaddressspaceisanareathatholdsthecodeanddataforsharedlibrariesAddresses•Typicaladdressescanbeashighas4billionor,innewercomputers,ashighas16quintillion(2elevatedtothe64power.)•Representofaddresses:oBase2oBase10oBase16••0x……•Addressesareclusteredinthreegroups:oallthelocalvariablesareclusteredtogetherooftheglobalvariables,thosethatareinitializedwhendeclaredareinonecluster,andthatthosethatarenotinitializedareinan

1 / 86
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功