Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!iuvax!cica!gatech!amdcad!cayman!tim
From: tim@cayman.amd.com (Tim Olson)
Newsgroups: comp.lang.c
Subject: Re: Declaration within a loop.
Message-ID: <27519@amdcad.AMD.COM>
Date: 27 Sep 89 19:11:03 GMT
References: <2085@hydra.gatech.EDU> <30174@news.Think.COM> <559@crdos1.crd.ge.COM>
Reply-To: tim@amd.com (Tim Olson)
Distribution: usa
Organization: Advanced Micro Devices, Austin, TX
Lines: 35
Summary:
Expires:
Sender:
Followup-To:

In article <559@crdos1.crd.ge.COM> davidsen@crdos1.UUCP (bill davidsen) writes:
| In article <30174@news.Think.COM>, barmar@kulla (Barry Margolin) writes:
| 
| |  Yes, a new i variable is declared.  However, at the end of each time
| |  through the loop it is "undeclared", so it can be deallocated.  Most C
| |  implementations will actually use the same memory location (probably
| |  on the stack) each time. 
| 
|   Most C compilers allocate space on the stack for this when the
| procedure is entered. It therefore is not a practical thing to do to
| save space. The most common use is to correct for having forgotten to
| declare a variable at the start of a procedure.

But sometimes it is better to declare the variable in the block to
limit its scope rather than to make every variable visible to the
entire function.  For example, say I want to debug a function by
printing out a linked list at a certain point.  I can say something
like:

	.
	.
#ifdef DEBUG
	{
		struct list *p;
		for (p=my_list; p; p=p->next)
		    printf("\t%d\n",p->value);
        }
#endif

and I have limited my changes to one area.


	-- Tim Olson
	Advanced Micro Devices
	(tim@amd.com)