Declarations in switches, errors

Barry Margolin barmar at kulla
Sun Oct 1 03:42:23 AEST 1989


In article <1989Sep30.052000.13719 at utzoo.uucp> henry at utzoo.uucp (Henry Spencer) writes:
>And, as Doug has pointed out, ironically, the behavior of initializers at
>the head of a case is not an example of this sort of thing.  Such
>initializers *do* *not* get executed in any conforming implementation.

Well, since the initial value of automatic variables is undefined, a
conforming implementation COULD execute them.  A strictly conforming
program cannot take advantage of this, though.

Here's something I think is closer to the original poster's complaint:
why does the standard permit initializers in declarations at the head
of a switch body, if they are required to be ignored?  If they were
disallowed, then user's wouldn't be tempted to think they would be
executed.

The original poster postulated that it should be easy for
implementations to initialize these variables, since it must allocate
space for them.  As was mentioned by several people in a different
chain (which might have been in comp.lang.c), many C compilers
allocate space for all the auto variables in a function when the
function is entered; no actual allocation and deallocation takes place
when entering and exiting internal blocks (this is possible because C
doesn't allow variable-sized automatic arrays).  Therefore, jumping to
a label inside a block doesn't have to do any allocation.  Thus, a
compiler can treat a variable declaration with an initializer as if
the declaration without the initializer were located in the outermost
block (this is a simplification, as it ignores name conflicts) and the
initializer were just an assignment statement.
Barry Margolin, Thinking Machines Corp.

barmar at think.com
{uunet,harvard}!think!barmar



More information about the Comp.std.c mailing list