Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Returning ptrs to local function variables

Status
Not open for further replies.

RealityVelJackson

Programmer
Aug 6, 2005
10
0
0
US
I was under the impression that one shouldn't return pointers to variables that are created inside of a function, unless the variable was static or allocated on the heap. Why does the following code work fine ? In foo1() since
str points to a string-literal, I guess the string-literal is allocated in a memory location outside of the stack frame, so it won't be deallocated when the function returns.
If this is true then I have no problem here.
I'm confused by the fact that the value in int x is not wiped-out when foo2() returns. My understanding is that int x is created on the stack-frame and deallocated by the time control is passed back to main(). How could a pointer to a deallocated auto variable still retrieve the value ?

Code:
#include <stdio.h>

char* foo1();
int* foo2();

int main(){

	char* s;
	int* d;

	s = foo1();
	printf("%s\n", s);

	d = foo2();
	printf("%d\n", *d);

	return 0;
}

char* foo1(){

	char* str = "hello";
	return str;
}

int* foo2(){

	int x = 8;
	return &x;
}
 
Because you're returning "by value" effectively.
The literal "hello" is statically allocated and will be available for the duraction of the life of the code.
Th char* str get's assigned the address of the start of this string.
The "return str" returns the VALUE of "str", not a reference to it. The VALUE of "str" is the ADDRESS of the literal string "hello" which, as I said earlier, is safe.

Does that make sense?



Trojan.
 
My understanding is that, although the memory pointed to by &x has been deallocated it hasn't been overwritten by the time you come to print it. I would bet that
Code:
  d = foo2();
  /*Lots of other stuff*/
  .......
  .......
  printf("%d\n", *d);
would give random results.

Columb Healy
 
With foo2 I agree completely.
You are at serious risk if you return pointers to released stack space.
Bad idea.


Trojan.
 
In regards to foo1, sting literals are static memory.

In regards to foo2, it lived on the stack, and because C/C++ isn't a "safe" language you can get the old data untill it is overwritten (in this case, by another function call).

try adding a foo3, like this
Code:
void foo3()
{
  int x = 300, y = 200, z = 500;
  for(int i = 0; i < 10; i++){ //only here to stop optimizer 
    int a = x+y+z+i;           // may not be needed.
    printf("%d\n", a);         // but better safe then sorry
  } 
}

and change main to:
Code:
int main(){

    char* s;
    int* d;

    s = foo1();
    printf("%s\n", s);

    d = foo2();
    foo3()
    printf("%d\n", *d);

    return 0;
}

And when I compile it I get 300 from printing out d... no suprise there, as the new x is alloced at the same address (time in function) as the old x.

[plug=shameless]
[/plug]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top