Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: utzoo!mnetor!uunet!husc6!cmcl2!arizona!wendt
From: wendt@arizona.edu (Alan Lee Wendt)
Newsgroups: comp.lang.c
Subject: Re: How are local vars allocated?
Message-ID: <3031@megaron.arizona.edu>
Date: Sun, 29-Nov-87 22:45:05 EST
Article-I.D.: megaron.3031
Posted: Sun Nov 29 22:45:05 1987
Date-Received: Wed, 2-Dec-87 22:58:21 EST
References: <9367@mimsy.UUCP> <1633@megatest.UUCP> <2218@killer.UUCP>
Organization: U of Arizona CS Dept, Tucson
Lines: 25

Regarding whether k is allocated and deallocated on each iteration
of the loop: 

> > 	for (;;) {
> > 		int k;
> > 		...
> > 	}

As Richard H. states, the variable's scope is the block in which it
is created.  But this block does not include any expressions outside
of the curly braces, hence does not include the for initialization,
test and increment expression.  Therefore control is exiting this
block and re-entering, and the variable is being allocated and
deallocated repeatedly, at least as far as the language semantics go.
(A compiler may legally choose to re-use k's location while evaluating
the for-expressions).

To convince yourself that, the "block defined" can't possibly include the
for-expressions, you can look at the definition of a block.  Or, consider
the situation of the poor compiler writer if someone actually mentioned
the variable k inside one of the for-expressions.  The compiler writer
would be in a position of having to compile a reference to k before k
has been declared.

Alan W.