Is goto safe?
John Woods
john at frog.UUCP
Sat Jan 20 14:26:00 AEST 1990
In article <253 at usblues.UUCP>, tom at 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 at frog.UUCP, ...!mit-eddie!jfw, jfw at eddie.mit.edu
Happiness is Planet Earth in your rear-view mirror. - Sam Hurt
More information about the Comp.lang.c
mailing list