As far as my understanding and other referals i understood that when the address
of a local variable is returned the first call using this address may print the correct
value but if it is called after any other function may get undefined values.

but my program is returning the value that is equivalent to address.

please some one help me in understanding this.

int * first();
int * second();

int main()
{
	int *f, *s ;
	f = first();
	printf(" After calling first : value of f ( %p ) \n", f);
	printf(" After calling first : value of *f ( %x )\n", *f);
	s = second();
	printf(" After calling second : value of s ( %p ) \n", s);
	printf(" After calling second : value of *s ( %x )\n", *s);
	f = first();
printf(" After calling first second time : value of f ( %p ) \n", f);
printf(" After calling first second time : value of *f ( %x )\n", *f);
	return 0;
}	

int * first()
{
	int f_lcl = 0xAAAA ;
	printf("  In First  : value of f_lcl ( %x ) \n", f_lcl);
	printf(" In First : addr of f_lcl is ( %x ) \n", &f_lcl); 
	return &f_lcl ;
}

int * second()
{
	int s_lcl =  0xFFFF;
	printf(" In Second : value of s_lcl ( %x )\n", s_lcl);
	printf(" In Second : addr of s_lcl is ( %x ) \n", &s_lcl); 
	return &s_lcl ;
}

the out i am getting is :

In First : value of f_lcl ( aaaa )
In First : addr of f_lcl is ( bff4f2c4 )
After calling first : value of f ( 0xbff4f2c4 )
After calling first : value of *f ( bff4f2c4 )
In Second : value of s_lcl ( ffff )
In Second : addr of s_lcl is ( bff4f2c4 )
After calling second : value of s ( 0xbff4f2c4 )
After calling second : value of *s ( bff4f2c4 )
In First : value of f_lcl ( aaaa )
In First : addr of f_lcl is ( bff4f2c4 )
After calling first second time : value of f ( 0xbff4f2c4 )
After calling first second time : value of *f ( bff4f2c4 )

Recommended Answers

All 5 Replies

Try running this modified version of your program...What happened to the values from the stack?

#include <stdio.h>
#include <stdlib.h>

char ch[] = "Don't do this, passing back the value of a local variable is dangerous!";

int * first();

void myfunc(char *s, int x)
{
	fprintf(stdout, "%s, %d\n", ch, x);
}

int main()
{
	int *f;

	f = first();

	printf(" After calling first : value of f ( %p ) \n", (void*)f);
	printf(" After calling first : value of *f ( %x )\n", *f);

	myfunc(ch, 3456);

	printf(" After calling first : value of f ( %p ) \n", (void*)f);
	printf(" After calling first : value of *f ( %x )\n", *f);

	return 0;
}	

int * first()
{
	int f_lcl = 0xAAAA ;
	printf("  In First  : value of f_lcl ( %x ) \n", f_lcl);
	printf(" In First : addr of f_lcl is ( %p ) \n", (void*)&f_lcl); 
	return &f_lcl ;
}

My output:

In First : value of f_lcl ( aaaa )
In First : addr of f_lcl is ( 0x7fffc032a14c )
After calling first : value of f ( 0x7fffc032a14c )
After calling first : value of *f ( aaaa )
Don't do this, passing back the value of a local variable is dangerous!, 3456
After calling first : value of f ( 0x7fffc032a14c )
After calling first : value of *f ( 0 )

As far as my understanding and other referals i understood that when the address
of a local variable is returned the first call using this address may print the correct
value but if it is called after any other function may get undefined values.

but my program is returning the value that is equivalent to address.

please some one help me in understanding this.

int * first();
int * second();

int main()
{
	int *f, *s ;
	f = first();
	printf(" After calling first : value of f ( %p ) \n", f);
	printf(" After calling first : value of *f ( %x )\n", *f);

the out i am getting is :

In First : value of f_lcl ( aaaa )
In First : addr of f_lcl is ( bff4f2c4 )
After calling first : value of f ( 0xbff4f2c4 )
After calling first : value of *f ( bff4f2c4 )
)

f is the address of the local variable, correct. The local variable has been allocated at the stack at the time first was running. Now you invoke printf. The parameters passed to printf are allocated at the stack as well, overwriting whatever may have left there. By pure luck (*) the used-to-be local variable gets overwritten by the second parameter of the call. So, after the first call to printf this stack location contains f, that is the address. When you call printf the second time, *f is evaluated prior to moving parameters to the stack - which of course yields an address.

And never ever return a local address in a production code.

(*) I do not know if this needs an explanation. If it does, ask.

*f is evaluated prior to moving parameters to the stack - which of course yields an address.

i dont understand the above statement;

Doesn't anyone see a problem with this :

int * first()
{
	int f_lcl = 0xAAAA ;
	printf("  In First  : value of f_lcl ( %x ) \n", f_lcl);
	printf(" In First : addr of f_lcl is ( %p ) \n", (void*)&f_lcl); 
	return &f_lcl ;
}

He is returning the address of a temporary variable, i.e it gets
destroyed after it goes out of scope, which is at the end of the function,
so the behavior is undefined.

commented: Exactly! +18
commented: Exactly! +6
int * first(){	int f_lcl = 0xAAAA ;	printf("  In First  : value of f_lcl ( %x ) \n", f_lcl);	printf(" In First : addr of f_lcl is ( %p ) \n", (void*)&f_lcl); 	return &f_lcl ;}

the warning:returning address of temporary.
if the temporary out of scope, it can be covered by other local variable.
That's why first call work, after show you a undefined values

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.