M
Myth__Buster
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;
}
Win32 O/P:
&a = 0022FF74
&b = 0022FF70
&c = 0022FF6C
Fedora Core Linux O/P:
&a = 0xbfaec044
&b = 0xbfaec040
&c = 0xbfaec03c
Here, I think that C could be same as B since the respective memory
becomes reusable once the variable 'b' goes out of scope, But, the
behavior of this program differs from what I think as above when
tested
on platforms - Windows XP and Fedora Core Linux with GCC versions
3.4.2 (Thread model - win32) and 3.4.6 (Thread model - posix)
respectively.
This behavior looks like being closely related to the implementation
of the compiler in deciding the memory utilization for the local
variables. However, it would be interesting to know whether are
there any restrictions imposed by Standard C(C99) in doing so(reuse)
as
far as the language "C" is concerned.
Cheers.
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;
}
Win32 O/P:
&a = 0022FF74
&b = 0022FF70
&c = 0022FF6C
Fedora Core Linux O/P:
&a = 0xbfaec044
&b = 0xbfaec040
&c = 0xbfaec03c
Here, I think that C could be same as B since the respective memory
becomes reusable once the variable 'b' goes out of scope, But, the
behavior of this program differs from what I think as above when
tested
on platforms - Windows XP and Fedora Core Linux with GCC versions
3.4.2 (Thread model - win32) and 3.4.6 (Thread model - posix)
respectively.
This behavior looks like being closely related to the implementation
of the compiler in deciding the memory utilization for the local
variables. However, it would be interesting to know whether are
there any restrictions imposed by Standard C(C99) in doing so(reuse)
as
far as the language "C" is concerned.
Cheers.