Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
5.00/5 (3 votes)
I am progressing with my C++ learning. I am stuck with the following code.
void ChangeData(int** ptr){<br />	int ab = 1000;  // scope in this method<br />	*ptr = &ab;<br />}<br /><br />int _tmain(int argc, _TCHAR* argv[])<br />{<br />	int a = 10;<br />	int* aPtr = &a;<br />	ChangeData(&aPtr);<br />	cout << *aPtr << endl;<br />	return 0;<br />}
In the ChangeData method, I am assigning address of a local variable to the supplied parameter. When ChangeData returns to the main, I believe variable ab will get removed from the stack. I was expecing an error when I run the above application, but the above one works!

I am confused how this is happening? If ab is removed from stack, which address the pointer holds and how I am getting correct value?
Posted

The variable is destroyed, but not the contents.
You know the abbress s you can continue to check it's value...but, as you sad, it is wrong, so do not do it. The system can use the same locatio of that variable in any moment after that the variable is destroyed, so the contentents will be soon invalid.

 
Share this answer
 
Christian Flutcher wrote:
I am confused how this is happening?


Because you were lucky (or in your case, not :) ). In your ChangeData function, your local variable has a certain address which you store in your ptr variable. When the function exits, your ptr variable still holds the same address but as you said, the variable has been 'removed' from the stack. But remove simply means that this memory can be used for other purposes, which in your case you don't so the memory is not overwritten (and still containing the same value). If now you would do other things before printing the value, chances are that it will not work anymore.

 
Share this answer
 
Christian Flutcher wrote:
If ab is removed from stack, which address the pointer holds and how I am getting correct value?


Actually, it is not "removed". That particular memory location is being marked as "available for use". But until you use it, the older value exists. :)

 
Share this answer
 
To add to what others said, try this with and without the printf(...) within the ChangeData(...)

C#
void ChangeData(int **p)
{
    int g = 100;
    *p = &g;
    printf("changed to %d\n", **p);
}
void main()
{
    int *q;
    ChangeData(&q);
    printf("changed to %d\n", *q);
}


Values written into a RAM address are always "logically removed" in that the language won't attempt to use it after it has finished its "life cycle". That doesn't mean that you cannot touch them at all. However, they may also be "physically removed" in that they may be overwritten due to other operations in the same or different application. It is safe and correct to not access it as the value still remaining cannot be counted upon.
 
Share this answer
 
Comments
Cedric Moonen 5-Aug-10 6:56am    
Did you realize that the question was posted in september 2008 ? Chances are that the OP is not interested in the answer anymore...


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