Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!husc6!uwvax!oddjob!gargoyle!ihnp4!laidbak!daveb From: daveb@laidbak.UUCP (Dave Burton) Newsgroups: comp.lang.c Subject: Re: How are local vars allocated? Message-ID: <1251@laidbak.UUCP> Date: Wed, 18-Nov-87 00:41:08 EST Article-I.D.: laidbak.1251 Posted: Wed Nov 18 00:41:08 1987 Date-Received: Sat, 21-Nov-87 11:59:11 EST References: <189@dalcsug.UUCP> <7401@prls.UUCP> Reply-To: daveb@laidbak.UUCP (Dave Burton) Organization: is pretty bad/My method of Lines: 75 In article <7401@prls.UUCP> gardner@prls.UUCP (Robert Gardner) writes: >Is there any danger of stack overflow with the following construct: > >for(;;) { > int k; > etc. etc. >} >(assume the loop eventually exits, of course). >In other words, is k allocated only once at the beginning of the >procedure containing this construct, or is it allocated each time >the {} block is entered? Is the answer implementation dependent? In response to your more general question (subject line), the answer is: Local (auto) variables in C are placed on a stack dynamically at function invocation time. This is the reason you cannot rely on the initial value of such a variable without an explicit initializer. These locals are destroyed upon return from the function. The usual implementation in a stack oriented computer is to point some register to the current stack location, then subtract a constant to the stack pointer to create an empty region which the locals will inhabit. Upon return, the stack pointer will be reset to the saved location, and the arguments 'popped' off (quite often, the popping action is accomplished by adding a value equivalent to the amount of storage required by the argument). Blocks '{statement;...}' define a new scope, but not a new invocation. Most compilers will create room for variables defined in a block when space is allocated for the function's local variables. Even if some implementation behaves this way, you are guaranteed the auto variable will be destroyed upon exit from the block, in the same manner as that described above. Your for loop is an example of a block. As a further example, the following program: main() { int a; a = 2; { int a; a = 1; printf("%d\n", a); } printf("%d\n", a); } produces the (unsurprising) output: 1 2 The variable 'a' in the inner block has a different scope than the variable in the outer block (main()). It is in a different 'name space'. My compiler (pcc based 68k) reserves room on the stack upon entry to the function main() via the link instruction, and destroys this room upon return via unlk. Machines without such instructions typically add and subtract compile-time calculated values from the stack pointer. This is correct for a K&R and ANSI conforming compiler. Be warned that not all compilers behave properly with name spaces, however, and the above program may not work due to symbol redefinition. (BTW, if you name your variables in like manner, you get what you deserve.) -- --------------------"Well, it looked good when I wrote it"--------------------- Verbal: Dave Burton Net: ...!ihnp4!laidbak!daveb V-MAIL: (312) 505-9100 x325 USSnail: 1901 N. Naper Blvd. #include Naperville, IL 60540