您好,欢迎访问三七文档
当前位置:首页 > 幼儿/小学教育 > 小学教育 > 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
本文标题:ssd6复习PPT_lecture3
链接地址:https://www.777doc.com/doc-3391424 .html