Click here to Skip to main content
15,891,923 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
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.

C#
int * ret()
{
    int a = 10;
    return &a;
}

int _tmain(int argc, _TCHAR* argv[])
{
    int *i = ret();
    cout<<*i;
    return 0;
}


Thanks in advance.
Posted

Go look at this thread[^].
You are just lucky that the value hasn't yet been overwritten.
Try this and see what you get:
C#
int _tmain(int argc, _TCHAR* argv[])
{
    int *i = ret();
    cout<<*i;
    cout<<*i;
    return 0;
}


Peter
 
Share this answer
 
Comments
[no name] 11-Jul-12 2:35am    
I am surprised. Thanks 5+
[no name] 11-Jul-12 2:39am    
But, why it gives wrong output when I again try to dereference it?
Peter_in_2780 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.
[no name] 11-Jul-12 3:25am    
OK. Thanks Peter.
Espen Harlinn 11-Jul-12 4:32am    
5'ed!

reminds me of: http://www.codeproject.com/Messages/4297101/Just-a-Cplusplus-tutorial.aspx
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

C++
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;
}
 
Share this answer
 
v2
Comments
[no name] 11-Jul-12 2:52am    
5!
Reza Oruji 12-Jul-12 14:50pm    
my 5
why it might crash?
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.
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900