資源描述:
《C Traps and Pitfalls.pdf》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在學(xué)術(shù)論文-天天文庫。
1、CTrapsandPitfalls*AndrewKoenigAT&TBellLaboratoriesMurrayHill,NewJersey07974ABSTRACTTheClanguageislikeacarvingknife:simple,sharp,andextremelyusefulinskilledhands.Likeanysharptool,Ccaninjurepeoplewhodon'tknowhowtohandleit.ThispapershowssomeofthewaysCcani
2、njuretheunwary,andhowtoavoidinjury.0.IntroductionTheClanguageanditstypicalimplementationsaredesignedtobeusedeasilybyexperts.Thelan-guageisterseandexpressive.Therearefewrestrictionstokeeptheuserfromblundering.Auserwhohasblunderedisoftenrewardedbyaneffec
3、tthatisnotobviouslyrelatedtothecause.Inthispaper,wewilllookatsomeoftheseunexpectedrewards.Becausetheyareunexpected,itmaywellbeimpossibletoclassifythemcompletely.Nevertheless,wehavemadearoughefforttodosobylookingatwhathastohappeninordertorunaCprogram.We
4、assumethereaderhasatleastapassingacquaintancewiththeClanguage.Section1looksatproblemsthatoccurwhiletheprogramisbeingbrokenintotokens.Section2fol-lowstheprogramasthecompilergroupsitstokensintodeclarations,expressions,andstatements.Section3recognizesthat
5、aCprogramisoftenmadeoutofseveralpartsthatarecompiledseparatelyandboundtogether.Section4dealswithmisconceptionsofmeaning:thingsthathappenwhiletheprogramisactuallyrunning.Section5examinestherelationshipbetweenourprogramsandthelibraryroutinestheyuse.Insec
6、tion6wenotethattheprogramwewriteisnotreallytheprogramwerun;thepreprocessorhasgottenatitfirst.Finally,section7discussesportabilityproblems:reasonsaprogrammightrunononeimplementa-tionandnotanother.1.LexicalPitfallsThefirstpartofacompilerisusuallycalledal
7、exicalanalyzer.Thislooksatthesequenceofcharac-tersthatmakeuptheprogramandbreaksthemupintotokens.Atokenisasequenceofoneormorechar-actersthathavea(relatively)uniformmeaninginthelanguagebeingcompiled.InC,forinstance,thetoken->hasameaningthatisquitedistinc
8、tfromthatofeitherofthecharactersthatmakeitup,andthatisindependentofthecontextinwhichthe->appears.Foranotherexample,considerthestatement:if(x>big)big=x;Eachnon-blankcharacterinthisstatementisaseparatetoken,exceptforthekeywordifandthetwoi