Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watmath!clyde!burl!codas!killer!richardh From: richardh@killer.UUCP Newsgroups: comp.lang.c Subject: Re: How are local vars allocated? Message-ID: <2218@killer.UUCP> Date: Wed, 25-Nov-87 19:05:14 EST Article-I.D.: killer.2218 Posted: Wed Nov 25 19:05:14 1987 Date-Received: Sun, 29-Nov-87 04:18:54 EST References: <9367@mimsy.UUCP> <1633@megatest.UUCP> <1987Nov22.085210.20641@sq.uucp> Organization: The Unix(R) Connection, Dallas, Texas Lines: 43 Summary: this var should be allocated only once In article <1987Nov22.085210.20641@sq.uucp>, msb@sq.uucp (Mark Brader) writes: > There have been several followups regarding the code: > > for (;;) { > int k; > ... > } > ... > 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, No it doesn't! It requests that a variable "k" be allocated and be visible for the duration of execution of the block defined by the "{}" pair. To quote H&S: "An object is said to have _local extent_ when (in the case of C) it is created upon entry to a block or function and is destroyed upon exit from the block or function." p. 57, sec. 4.2.7, third paragraph, ed. 1. Even if it is allocated on entry to the block, it will only be allocated 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. > > 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 Yes it is! The variable's scope is the block in which it is defined. richard hargrove ...!killer!richardh ------------------- Found on a Post-It attached to the front of a terminal in a deserted office in downtown Wichita: "Aunty Em, Hate you. Hate Kansas. Took the dog. Dorothy" ---------------------------------------------