Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!cs.utexas.edu!samsung!zaphod.mps.ohio-state.edu!think!mintaka!mit-eddie!bu.edu!bu-cs!lectroid!cloud9!jjmhome!cpoint!frog!john From: john@frog.UUCP (John Woods) Newsgroups: comp.lang.c Subject: Re: Is goto safe? Keywords: C Message-ID: <11321@frog.UUCP> Date: 20 Jan 90 03:26:00 GMT References: <253@usblues.UUCP> Organization: Misanthropes-R-Us Lines: 28 In article <253@usblues.UUCP>, tom@usblues.UUCP (Tom Markson) writes: > Without getting into style issues, is the goto statement in C safe? Yes, the goto statement is perfectly safe. It's your programs that are in trouble. > For instance, If I do the following, am I guarenteed that no stack > crashes will occur: That is about the only evil which won't befall you. > main() { { int i; i = 0; /* program adjusted for (near) maximum terseness */ > inside: printf("inside:%d\n",++i); goto outside; } > { int j; j = 0; > outside: printf("outside: %d\n",++j); goto inside; } } > Question: Is this always true. Will it always run as expected with i and j > incrementing from 0 to ...? > It is not guaranteed. Once you goto out of the scope of a variable, it is not obligated to retain its value. It would be legal for a compiler to use the same space for both i and j, since their scopes are mutually exclusive. Note, by the way, that j is never assigned an actual value in the sequential execution of the program. That it starts with a value of 0 is accidental. -- John Woods, Charles River Data Systems, Framingham MA, (508) 626-1101 ...!decvax!frog!john, john@frog.UUCP, ...!mit-eddie!jfw, jfw@eddie.mit.edu Happiness is Planet Earth in your rear-view mirror. - Sam Hurt