Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ C
Please see the code, in this I have created 'local' varialbe, assigned value and returns its address. And it works. But how. 'a' is local varible, as function return should it not get freed. Or it is just working because compiler has not collected the memory.
 
int * ret()
{
    int a = 10;
    return &a;
}
 
int _tmain(int argc, _TCHAR* argv[])
{
    int *i = ret();
    cout<<*i;
    return 0;
}
 
Thanks in advance.
Posted 10-Jul-12 21:23pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Go look at this thread[^].
You are just lucky that the value hasn't yet been overwritten.
Try this and see what you get:
int _tmain(int argc, _TCHAR* argv[])
{
    int *i = ret();
    cout<<*i;
    cout<<*i;
    return 0;
}
 
Peter
  Permalink  
Comments
Pranit Kothari at 11-Jul-12 2:35am
   
I am surprised. Thanks 5+
Pranit Kothari at 11-Jul-12 2:39am
   
But, why it gives wrong output when I again try to dereference it?
Peter_in_2780 at 11-Jul-12 3:14am
   
Because your first call to cout::operator<< overwrites that memory (after it has fetched the argument *i).
 
Read the whole thread I referenced. No one spells it out in small words, but several of us comment on what's happening.
Pranit Kothari at 11-Jul-12 3:25am
   
OK. Thanks Peter.
Espen Harlinn at 11-Jul-12 4:32am
   
5'ed!
 
reminds me of: http://www.codeproject.com/Messages/4297101/Just-a-Cplusplus-tutorial.aspx
Peter_in_2780 at 11-Jul-12 5:35am
   
Funny that. See the link in my answer? ;)
Espen Harlinn at 11-Jul-12 5:37am
   
Ahh, I see ... :laugh:
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

You are absolutely correct at this point of pointer to local variable.
 
the pointer to a local variable will keep pointing to the memory that will has gone out of scope.
 
The reason this might be working is by pure chance, the memory that the returned pointer is pointing has still not got anything else overwritten on it(since nothing is written on stack still) and thus it is working. If you keep this code in the middle of more code then it will definitely give incorrect results and it might even crash.
 
try this for instance
 
int * ret()
{
    int a = 10;
    return &a;
}
 
int _tmain(int argc, _TCHAR* argv[])
{
    int *i = ret();
    string s = "some rando, string to test";
    cout<<*i;
    return 0;
}
  Permalink  
v2
Comments
Pranit Kothari at 11-Jul-12 2:52am
   
5!
rezaricky at 12-Jul-12 14:50pm
   
my 5
why it might crash?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

As some people already have mentioned correctly. The variable ended up on the stack, which grows the more you nest function calls. The stack shrinks when the functions returns. The depth of the nested function calls determines when it will overwritten by function calls that comes after.
 
If the local variable had been declared static int a, the variable wouldn't have been stored on the stack. But of course, you will get your punched if you ever return pointers to internal static variables. Static functional variables are of course not multithread safe.
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 325
1 Sergey Alexandrovich Kryukov 289
2 CPallini 275
3 DamithSL 260
4 Maciej Los 215
0 OriginalGriff 5,455
1 DamithSL 4,457
2 Maciej Los 3,885
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,115


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 11 Jul 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100