Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Scope of identifiers Date: Wed, 14 Jun 2017 09:19:14 -0700 Organization: None to speak of Lines: 45 Message-ID: References: <081872d4-3f9d-0004-8c0d-25a274bba7bb@verizon.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="96bbf95725af2559ab0735ee54475e20"; logging-data="28600"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19r4WV7RkwNRPb5lKlCJlB1" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:J2T5ri+k9SYlFEQAqVbfiPv2VsI= sha1:ebGon48aXih3Amhoj/NhzMY5Zk8= Xref: csiph.com comp.lang.c:112196 supercat@casperkitty.com writes: > On Tuesday, June 13, 2017 at 11:02:59 PM UTC-5, David Kleinecke wrote: >> In other words in a primitive pre-C there was no initialization >> and the declarations could be thought of as simultaneous and all >> the scopes as the same. This simplicity is lost when initialization >> was introduced. > > Under C89, a single-pass implementation given: > > int x = expressionYieldingLotsOfCode; > int y; > > could generate code: > > bump stack pointer to leave space for x > evaluate expresstionYieldingLotsOfCode > bump stack pointer to leave space for y > > The compiler wouldn't need to know about y, or much space it would take, > when generating code for the big expression. Under C99, however, a > compiler has to handle: > > someLabel: > arbitraryStatement; > int x = expressionYieldingLotsOfCode; > int y; > > If jumping before a declaration would end a variable's lifetime, an > implementation could simply precede the jump with code that would reset > the stack pointer to the value it had the first time it ran the > arbitrary statement. Under C99 rules, however, space would need to > remain reserved for y the second time the code runs. Under C90, as under C99, the lifetime of an object with automatic storage duration begins on entry to the enclosing block. A C90 compiler could defer allocation until control reaches the declaration, but that's an optimization, not part of the visible semantics. A C99 compiler can perform exactly the same optimization if there are no labels preceding the declaration. -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"