tom@usblues.UUCP (Tom Markson) (01/17/90)
I posted this previously, but I don't think it went out. Apologies if it did. Without getting into style issues, is the goto statement in C safe? For instance, If I do the following, am I guarenteed that no stack crashes will occur: main() { { int i; i=0; inside: i++; printf("inside:%d\n",i); goto outside; } { int j; j=0; outside: j++; printf("outside: %d\n",j); goto inside; } } My C compiler (xenix) codes this and it runs. i and j keep their values. The .s file indicates that all stack space is allocated at the start of a function. Question: Is this always true. Will it always run as expected with i and j incrementing from 0 to ...? Note: According to K & R, the one restriction to goto is that it must be within a func. Can I count on this being the only restriction? Thank you in advance. -- Tom Markson ...!uunet!usblues!tom ...!cbmvax!amix!blekko!usblues!tom
maart@cs.vu.nl (Maarten Litmaath) (01/19/90)
In article <253@usblues.UUCP>, tom@usblues.UUCP (Tom Markson) writes: \..., am I guarenteed that no stack \crashes will occur: [...] No stack crashes will occur, but I'm not sure about brain crashes. -- What do the following have in common: access(2), SysV echo, O_NONDELAY? | Maarten Litmaath @ VU Amsterdam: maart@cs.vu.nl, uunet!mcsun!botter!maart
john@frog.UUCP (John Woods) (01/20/90)
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