Path: utzoo!mnetor!uunet!husc6!mailrus!tut.cis.ohio-state.edu!bloom-beacon!gatech!mcnc!decvax!cca!g-rh From: g-rh@cca.CCA.COM (Richard Harter) Newsgroups: comp.lang.c Subject: Re: goto in C (was fallthrough) Message-ID: <25385@cca.CCA.COM> Date: 8 Mar 88 07:54:20 GMT References: <222965b9@ralf.home> <2403@umd5.umd.edu> <25200@cca.CCA.COM> <25348@cca.CCA.COM> <740@cresswell.quintus.UUCP> Reply-To: g-rh@CCA.CCA.COM.UUCP (Richard Harter) Organization: Computer Corp. of America, Cambridge, MA Lines: 56 Keywords: C case fallthrough goto In article <740@cresswell.quintus.UUCP> ok@quintus.UUCP (Richard A. O'Keefe) writes: >I very often write code of the form > [Elegant stylised error handling mechanism example deleted.] Pretty. I am going to have to think about that. >If you are trying to write code that other people can use, >it really isn't good manners to lose chunks of memory just >because you couldn't open a file. One of these per library >package, and one per program, doesn't seem excessive. Not good manners is a weak term -- particularly in code which is designed to run as a background process indefinitely. The inadequacies of malloc and C memory allocation is a topic for another time. >This is a sufficiently stylised approach that it could be replaced by >some other mechanism. (Nested exception handlers? C++ destructors? >unwind-protect?) But in a language like C, why not just keep the goto? >{Actually, a break-from-labelled-statement would work too, but that's >just another goto.} Not really. The model for break-from-labelled-statement is a stack of return points, a generalization of the procedure return point stack. With such a command you can only goto a point in the stack; you are just going up several levels instead of one. With a real goto you can transfer to a point which is not in the stack. [And, yes, I know that you can simulate goto's with a breaks. You can program recursion in Fortran too.] As to your general point. Without some sort of mechanism for error handling escapes, one needs a goto to build it by hand. C has a goto, it doesn't have generalized escapes, and that's the way it is. It would be awkward to simply delete the goto from C, to say nothing of breaking a lot of existing code. [Whether this code should have been written that way is another matter.] I am still not convinced that the goto should be retained in a successor language (e.g. D) -- it does seem to me that one could have a more structured mechanism such as the break-from-labelled-statement and dispense with goto's entirely. >There's another reason for retaining the goto. Look at the output of >YACC some time. I counted five labels in yyparse(), but may have >missed some. It isn't just people who write C, you know! Why shouldn't >people be allowed to write compilers which generate C? This may be a good argument, which I will pass onto those who are better acquainted with the problems of writing programs which write programs. -- In the fields of Hell where the grass grows high Are the graves of dreams allowed to die. Richard Harter, SMDS Inc.