Re: Question on switch block scope
From: Kevin Bracey (kevin.bracey_at_tematic.com)
Date: 11/19/03
- Next message: Dan Pop: "Re: memcpy/memmove"
- Previous message: Morris Dovey: "Re: Header include order"
- In reply to: Sheldon Simms: "Re: Question on switch block scope"
- Next in thread: Kevin P. Fleming: "Re: Question on switch block scope"
- Reply: Kevin P. Fleming: "Re: Question on switch block scope"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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/
- Next message: Dan Pop: "Re: memcpy/memmove"
- Previous message: Morris Dovey: "Re: Header include order"
- In reply to: Sheldon Simms: "Re: Question on switch block scope"
- Next in thread: Kevin P. Fleming: "Re: Question on switch block scope"
- Reply: Kevin P. Fleming: "Re: Question on switch block scope"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|