Path: utzoo!attcan!uunet!ginosko!uakari.primate.wisc.edu!ames!think!kulla!barmar From: barmar@kulla (Barry Margolin) Newsgroups: comp.std.c Subject: Re: Declarations in switches, errors Message-ID: <30540@news.Think.COM> Date: 30 Sep 89 17:42:23 GMT References: <561@crdos1.crd.ge.COM> <11158@smoke.BRL.MIL> <637@crdos1.crd.ge.COM> <1989Sep30.052000.13719@utzoo.uucp> Sender: news@Think.COM Organization: Thinking Machines Corporation, Cambridge MA, USA Lines: 32 In article <1989Sep30.052000.13719@utzoo.uucp> henry@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@think.com {uunet,harvard}!think!barmar