Explanation, please!

T. William Wells bill at proxftl.UUCP
Mon Aug 29 15:36:24 AEST 1988


In article <11997 at steinmetz.ge.com> davidsen at crdos1.UUCP (bill davidsen) writes:
: [ a do loop within a switch, with cases all through the do loop ]
:
: This does not seem to be clearly covered in the latest dpANS (section
: 3.6). The issue is if it is legal *and defined* to jump into a loop.

It is defined, and the same as it always has been.  To quote from
section 3.1.2.4:

"Storage is guaranteed to be reserved for a new instance of such
an object [objects with *automatic storage duration*] on each
normal entry into the block in which it is declared, or on a jump
from outside the block to a label in the block or in an enclosed
block.  If an initialization is specified for the value stored in
the object, it is performed on each normal entry, but not if the
block is entered by a jump to a label."

: While a case label behaves just like any other label, it's not clear
: what a jump into a loop implies, and I don't see that the existing
: standard is any clearer on the topic than the discussion was three years
: ago.
:
:       i = 15;
:       if (j < 10) goto BadMove;
:
:       /* more code here */
:
:       do {
:         int i = 7;
:         int myvect[400];
:       BadMove: /* entry into block */
:         myvect[300] = i;
:         if (j < 5) return i;
:       } while (j-- > 302);

The above tells us that your example 1) allocates space for i and
myvect before or as the block is entered, and 2) does not
initialize `i' when the loop is entered via the goto, but does on
each subsequent iteration of the loop.

:   This is one of the rare times when I would like to be wrong, to be
: told "oh, it's there in a footnote, you can, or you can't, and this is
: what it does." If the action is implementation dependent then I
: (personally) wouldn't use the feature, since I value portability
: highly. Please tell me X3J11 didn't let this go unresolved for three
: years.

Nope.  All they did was move it from 3.6.2 (the equivalent of K&R
9.2) to 3.1.2.4.  I too have been bitten by not having the
standard say things in the expected place.  (Remember my
NULL!=NULL posting?  I got corrected by no less than dmr
himself!  Flaming red :-)

>From K&R section 9.2:

"Any initializations of auto or register variables are performed
each time the block is entered at the top.  It is currently
possible (but a bad practice) to transfer into a block; in that
case the initializations are not performed."

Nothing could be clearer, eh?

---
Bill
novavax!proxftl!bill



More information about the Comp.lang.c mailing list