po*************@gmail.com writes:
suppose i have piece of code like
main()
{
int i,j;
for(i=0;i<10;i++){
int k = i;
printf("%d %p = *%d\n",i,&k,k);
}
}
When i see the address of K its same in all iterations.That means K is
only defined once at a time.
Is it because each for loop execution is considered as separte
block,its allocating in the same address?
or is it allocated memory only once?
The name "k" refers to a distinct object for each iteration of the
loop. Conceptually, that object is created on entry to the block (the
"{") and destroyed on exit from the block (the "}"). Though there are
going to be 10 logically distinct objects, only one of them will exist
at any particular time. Similarly, if this code were inside a
function other than main, the variables "i" and "j" would exist only
while that function is executing. (The same thing applies to main,
but the concept is easier to understand if you consider a more
ordinary function.)
That's what the C language requires. It doesn't specify exactly what
a particular implementation must do to meet this requirement.
There are several ways to do this. Many, perhaps most,
implementations use a contiguous hardware stack for allocation of
local variables. In such an implementation, *either* shared space for
all instances of "k" is allocated on entry to the function, *or* space
for "k" is allocated and deallocated on each iteration of the loop.
In both cases, all instances of "k" are likely to have the same
address. This is just a consequence of the most natural way to
implement what the language requires; it is not itself required by the
language, and you should not depend on it.
For example, some implementations might allocate local storage on a
"heap", where successive allocations are not necessarily consecutive
in memory. In such an implementation, *if* "k" is allocated
separately each time the block is executed, then it could easily have
a different address on each iteration.
In any case, if you stick to what the language guarantees it's not
easy even to check whether all 10 "k"s have the same address. As soon
as "k" goes out of scope, even looking at its address without
dereferencing it invokes undefined behavior. You can, as you did,
display the address with printf and "%p", but the resulting text
string is implementation-defined, and it's not clear how much
information you can *portably* get out of it.
The best answer is just not to worry about it. Know that the compiler
will do what the standard requires it to do (unless it's buggy, which
does happen). If you're curious about how it does this, that's great;
curiosity is A Good Thing. But if your program actually depends on
the details, either you're doing something wrong or you're doing
something subtle and implementation-specific; in the latter rare case,
you'd better know exactly what you're doing.
(Somebody else in this thread already told you about the need to use
(void*)&k rather than just &k with the "%p" format. A couple of other
quibbles that aren't relevant to your question: use "int main(void)"
rather than "main()", and be sure you have a "#include <stdio.h>" at
the top of your program. You might be able to get away with "main()"
and without "#include <stdio.h>", but do it right anyway.)
--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"