Re: Question on switch block scope

From: Kevin Bracey (kevin.bracey_at_tematic.com)
Date: 11/19/03


Date: Wed, 19 Nov 2003 15:13:45 GMT

In message <pan.2003.11.14.21.57.22.328539@yahoo.com>
          Sheldon Simms <sheldonsimms@yahoo.com> wrote:

> On Fri, 14 Nov 2003 12:10:06 -0800, Minti wrote:
>
> > Sheldon Simms <sheldonsimms@yahoo.com> wrote in message news:<pan.2003.11.13.21.19.58.700529@yahoo.com>...
> >
> >> goto foo;
> >> {
> >> int x = 123;
> >> foo:
> >> printf("The value of x %d\n", x);
> >> }
> >
> > Thanks Sheldon, but could you tell me when is the memory for x
> > allocated. If it has to be allocated when we get into the block after
> > the statement
> >
> > goto foo;
> >
> > Then obviously we can't use x at any place. Or is it the case that
> > that space would be reserved for x, or any other variable within the
> > immediate scope of the switch block but it's value would not be
> > initialized for the given initialization value.
>
> We don't know or care when space for the object named x is reserved,
> but we know that x is in scope and accessible from the point of its
> declaration to the end of the block in which it is declared.

I think we do care, although not in this example. Minti's question is
answered by distinguishing the "lifetime" from the "scope".

x's lifetime extends from the start of its enclosing block to the end. Thus
in the following example:

     int *p = NULL;
     {
       label:
         if (p) printf("x = %d\n", *p);
         
         int x = 5;
         
         printf("x = %d\n", x);
         
         x = 10;
         
         if (!p)
         {
             p = &x;
             goto label;
         }
     }

You will get the output

     x = 5
     x = 10
     x = 5

Although x isn't in scope at the first printf, it still exists, and you
can reference it through the pointer p.

So, from the abstract machine's point of view, x is "allocated", but
uninitialised when the { } block is entered and "deallocated" when the block
is exited. It is initialised (or re-initialised) whenever execution reaches
its declaration/initialiser.

An actual implementation does not have to actually "allocate" or "deallocate"
x there - it may reserve space for it on function entry, for example,
regardless of whether the block is entered. But that's not the C programmer's
business. For you, the scope extends from the declaration to the end of the
block, and the lifetime from block entry to block exit.

PS - never write code like that
PPS - the example is C99
PPPS - a worse example is in the C99 rationale

-- 
Kevin Bracey, Principal Software Engineer
Tematic Ltd                                   Tel: +44 (0) 1223 503464
182-190 Newmarket Road                        Fax: +44 (0) 1223 503458
Cambridge, CB5 8HE, United Kingdom            WWW: http://www.tematic.com/


Relevant Pages

  • Re: Initialization is done before the program starts executing ?
    ... initialization is done once only, ... why the declaration is executed but the initialization is not? ... These two concepts are scope and execution flow. ...
    (comp.lang.c)
  • Re: Question on switch block scope
    ... > initialized for the given initialization value. ... but we know that x is in scope and accessible from the point of its ... declaration to the end of the block in which it is declared. ... For each different entity that an identifier designates, ...
    (comp.lang.c)
  • Re: 406 Not Acceptable (was Re: "--All You Zombies--" title)
    ... matter where the declaration occurs. ... I think you're confusing scope with allocation and/or lifetime. ... have scope everywhere in the object file. ...
    (rec.arts.sf.written)
  • Re: Question on switch block scope
    ... > x there - it may reserve space for it on function entry, for example, ... the scope extends from the declaration to the end of the ... and the lifetime from block entry to block exit. ... if 'x' was not in scope in the first line of the ...
    (comp.lang.c)
  • Re: C99: Suggestions for style(9)
    ... I do NOT want to see variable declaration "not at the top of a block". ... Neither are all declarations grouped together nor are declarations nicely connected to their initialisation and their scope is minimal. ... -then in alphabetical order; multiple ones per line are okay. ... Conditional initialization must of course come after the condition, and can not be in the same line as the declaration anyhow. ...
    (freebsd-hackers)