Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: utzoo!mnetor!uunet!husc6!hao!oddjob!mimsy!chris
From: chris@mimsy.UUCP (Chris Torek)
Newsgroups: comp.lang.c
Subject: Re: How are local vars allocated?
Message-ID: <9547@mimsy.UUCP>
Date: Thu, 26-Nov-87 19:52:37 EST
Article-I.D.: mimsy.9547
Posted: Thu Nov 26 19:52:37 1987
Date-Received: Sun, 29-Nov-87 17:54:51 EST
References: <9367@mimsy.UUCP> <1633@megatest.UUCP> <2218@killer.UUCP>
Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742
Lines: 52

In article <2218@killer.UUCP> richardh@killer.UUCP (Richard Hargrove) writes
a bunch of stuff that quotes H&S correctly, but misinterpets something
somewhere:
>> 	for (;;) {
>> 		int k;
>> 		...

>... requests that a variable "k" be allocated and be visible
>for the duration of execution of the block defined by the "{}" pair.

Good so far.

>Even if it is allocated on entry to the block, it will only be allocated
>once.

It will be `allocated' each time you enter the block.  When do you enter
the block?  Answer: once for each trip around the loop: this is probably
more than once.

>As has been pointed out, many compilers actually allocate the storage upon 
>entry to the function and limit the object's visibility to the defining
>block.

Right again.

>>But the value of "k" is NOT guaranteed to be retained from one iteration
>>to the next, and you must not assume it will be.

(The >> text is by Mark Brader, msb@sq.UUCP, and is correct.)

>Yes it is! The variable's scope is the block in which it is defined.

The scope is indeed the block in which it it is defined.  Here is
an example of *incorrect* usage:

	int i;

	for (i = 0; i < 10; i++) {
		int j;

		if (i == 0)
			j = 1;
		else
			j *= i;
		...
	}

This uses the undefined value of `j' for all but the first trip
around the loop.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7690)
Domain:	chris@mimsy.umd.edu	Path:	uunet!mimsy!chris