Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!sq!msb From: msb@sq.UUCP Newsgroups: comp.lang.c Subject: Re: How are local vars allocated? Message-ID: <1987Nov22.085210.20641@sq.uucp> Date: Sun, 22-Nov-87 08:52:10 EST Article-I.D.: sq.1987Nov22.085210.20641 Posted: Sun Nov 22 08:52:10 1987 Date-Received: Tue, 24-Nov-87 03:36:23 EST References: <9367@mimsy.UUCP> <1633@megatest.UUCP> Reply-To: msb@sq.UUCP (Mark Brader) Organization: SoftQuad Inc., Toronto Lines: 27 Checksum: 07917 There have been several followups regarding the code: for (;;) { int k; ... } but I think they have each either left out an important point, or wandered at length into side issues. So let me try. The above requests that a new variable "k" be created, and destroyed again, on each iteration of the loop. Since it is destroyed each time, obviously it can't cause a stack overflow after many iterations. Any sensible compiler will implement the creation and destruction virtually, which is to say, any actual stack operations will take place either when the loop is entered for the first time and exited for the last time, or when the function is entered and exited. Or perhaps "k" will be put in a register and there will be no stack operations at all. 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. If you want that, you have to declare "k" in a larger scope including the for-header. Mark Brader, SoftQuad Inc., Toronto, utzoo!sq!msb, msb@sq.com "Have you ever heard [my honesty] questioned?" "I never even heard it mentioned." -- Every Day's a Holiday