Quantcast
Channel: Active questions tagged return-value - Stack Overflow
Viewing all articles
Browse latest Browse all 207

Return address of local variable in C

$
0
0

Say I have the following two functions:

1

int * foo(){  int b=8;  int * temp=&b;  return temp;}

2

int * foo(){   int b=8;   return &b;}

I don't get any warning for the first one (e.g. function returns address of a local variable) but I know this is illegal since b disappears from the stack and we are left with a pointer to undefined memory.

So when do I need to be careful about returning the address of a temporary value?


Viewing all articles
Browse latest Browse all 207


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>