Myth__Buster <> writes:
> NOTE : I am using the terms "reuse" or "reusable" with respect to
> the object code generated at the compilation time.
>
> /*
> * Program to analyze whether it is meaningful to reuse the
> * stack memory once the respective variable goes out of
> * scope.
> */
>
> #include<stdio.h>
>
> int main()
> {
> int a; /* Let's say this has the address : A. */
> printf("\n &a = %p ", &a);
>
> {
> int b; /* Let's say this has the address : B. */
> printf("\n &b = %p", &b);
> }
>
> int c; /* Let's say this has the address : C. */
> printf("\n &c = %p", &c);
>
> return 0;
> }
The lifetimes of both a and c cover the entire block that encloses
their declarations. The lifetime of b covers the inner block in
which it's declared. Thus b and c have overlapping lifetimes,
and cannot (in the abstract machine) share the same address.
You might think that the lifetime of c starts at its declaration,
but C99 6.2.4p5 says otherwise; it exists starting on entry to
the block, but its value is indeterminate until its declaration
is reached. It's possible to contrive a program that stores the
address of c in an int* variable, then uses goto to branch to
a point before its declaration; that code can access the object
"before" its declaration.
If you hadn't displayed the addresses of the objects, the compiler
could store them in the same location, as long as the program's
visible behavior isn't affected by the optimization.
--
Keith Thompson (The_Other_Keith)
kst- <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"