Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ C++/CLI C MFC ATL WTL STL
I am progressing with my C++ learning. I am stuck with the following code.
void ChangeData(int** ptr){
	int ab = 1000;  // scope in this method
	*ptr = &ab;
}

int _tmain(int argc, _TCHAR* argv[])
{
	int a = 10;
	int* aPtr = &a;
	ChangeData(&aPtr);
	cout << *aPtr << endl;
	return 0;
}
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 23-Sep-08 0:42am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.

  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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


Because you were lucky (or in your case, not Smile | :) ). 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.

  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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. Smile | :)

  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

To add to what others said, try this with and without the printf(...) within the ChangeData(...)
 
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.
  Permalink  
Comments
Cedric Moonen at 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...
  Print Answers RSS
0 OriginalGriff 6,769
1 Sergey Alexandrovich Kryukov 6,338
2 DamithSL 5,408
3 Manas Bhardwaj 4,816
4 Maciej Los 4,195


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 5 Aug 2010
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