Variable scoping and pointers
-
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. WhenChangeData
returns to the main, I believe variableab
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? Ifab
is removed from stack, which address the pointer holds and how I am getting correct value? -
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. WhenChangeData
returns to the main, I believe variableab
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? Ifab
is removed from stack, which address the pointer holds and how I am getting correct value?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.
Russell
-
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. WhenChangeData
returns to the main, I believe variableab
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? Ifab
is removed from stack, which address the pointer holds and how I am getting correct value?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.
Cédric Moonen Software developer
Charting control [v1.5] OpenGL game tutorial in C++ -
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. WhenChangeData
returns to the main, I believe variableab
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? Ifab
is removed from stack, which address the pointer holds and how I am getting correct value?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. :)
Many are stubborn in pursuit of the path they have chosen, few in pursuit of the goal - Friedrich Nietzsche .·´¯`·->Rajesh<-·´¯`·. [Microsoft MVP - Visual C++]
-
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.
Cédric Moonen Software developer
Charting control [v1.5] OpenGL game tutorial in C++Cedric Moonen wrote:
Because you were lucky
:) Yes, I am.
Cedric Moonen wrote:
If now you would do other things before printing the value, chances are that it will not work anymore.
That makes sense. Thank you all for the help.
-
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. :)
Many are stubborn in pursuit of the path they have chosen, few in pursuit of the goal - Friedrich Nietzsche .·´¯`·->Rajesh<-·´¯`·. [Microsoft MVP - Visual C++]
Thanks Rajesh.
Rajesh R Subramanian wrote:
But until you use it, the older value exists
I understand. But how do I use it? I just want to test this. I have created a big array in the
main()
and assigned some values to each element thinking that it will use the stack space, but still my old value exist. I am sure that I am doing something wrong. Any ideas? -
Thanks Rajesh.
Rajesh R Subramanian wrote:
But until you use it, the older value exists
I understand. But how do I use it? I just want to test this. I have created a big array in the
main()
and assigned some values to each element thinking that it will use the stack space, but still my old value exist. I am sure that I am doing something wrong. Any ideas?Christian Flutcher wrote:
I understand. But how do I use it? I just want to test this.
You will not be able to write to a location after the control comes out of it's scope of existence, because now do not have write access to that location. If you are keen to see yourself rewriting the contents of that 'particular' portion of memory, try a placement new: Is there a way to force
new
to allocate memory from a specific area?[^]Christian Flutcher wrote:
I have created a big array in the main() and assigned some values to each element thinking that it will use the stack space, but still my old value exist.
Yes, the stack is simply 'big enough' to hold it all. Besides that, it is an unnecessary exercise to wipe off the contents of the memory location 'as and when' it is freed. Erasing the contents of the freed memory every time will degrade the performance severely. To tackle this, the memory manager marks that portion of memory as 'available for use', and this will be treated as free memory and the old contents will be replaced with new content, when the program writes to that portion of memory. Hope that helps. :)
Many are stubborn in pursuit of the path they have chosen, few in pursuit of the goal - Friedrich Nietzsche .·´¯`·->Rajesh<-·´¯`·. [Microsoft MVP - Visual C++]
-
Christian Flutcher wrote:
I understand. But how do I use it? I just want to test this.
You will not be able to write to a location after the control comes out of it's scope of existence, because now do not have write access to that location. If you are keen to see yourself rewriting the contents of that 'particular' portion of memory, try a placement new: Is there a way to force
new
to allocate memory from a specific area?[^]Christian Flutcher wrote:
I have created a big array in the main() and assigned some values to each element thinking that it will use the stack space, but still my old value exist.
Yes, the stack is simply 'big enough' to hold it all. Besides that, it is an unnecessary exercise to wipe off the contents of the memory location 'as and when' it is freed. Erasing the contents of the freed memory every time will degrade the performance severely. To tackle this, the memory manager marks that portion of memory as 'available for use', and this will be treated as free memory and the old contents will be replaced with new content, when the program writes to that portion of memory. Hope that helps. :)
Many are stubborn in pursuit of the path they have chosen, few in pursuit of the goal - Friedrich Nietzsche .·´¯`·->Rajesh<-·´¯`·. [Microsoft MVP - Visual C++]
Rajesh R Subramanian wrote:
Hope that helps
Indeed. Thanks for the help. :)
-
Christian Flutcher wrote:
I understand. But how do I use it? I just want to test this.
You will not be able to write to a location after the control comes out of it's scope of existence, because now do not have write access to that location. If you are keen to see yourself rewriting the contents of that 'particular' portion of memory, try a placement new: Is there a way to force
new
to allocate memory from a specific area?[^]Christian Flutcher wrote:
I have created a big array in the main() and assigned some values to each element thinking that it will use the stack space, but still my old value exist.
Yes, the stack is simply 'big enough' to hold it all. Besides that, it is an unnecessary exercise to wipe off the contents of the memory location 'as and when' it is freed. Erasing the contents of the freed memory every time will degrade the performance severely. To tackle this, the memory manager marks that portion of memory as 'available for use', and this will be treated as free memory and the old contents will be replaced with new content, when the program writes to that portion of memory. Hope that helps. :)
Many are stubborn in pursuit of the path they have chosen, few in pursuit of the goal - Friedrich Nietzsche .·´¯`·->Rajesh<-·´¯`·. [Microsoft MVP - Visual C++]
Rajesh R Subramanian wrote:
To tackle this, the memory manager marks that portion of memory as 'available for use',
I'm not an expert on the stack but I guess there's even no need to mark some memory blocks as available. It is a stack, so it means that things that are 'pushed' (e.g. memory allocated on the stack) last on the stack needs to be 'popped' (freed) first. So, simply having a pointer which points to the top of the stack is enough. That's also the reason why allocating memory on the stack is much faster: you just allocate a certain amount of memory on the top of the stack. No need to search in different memory regions to find a block which is big enough (which is the case when you allocate memory on the heap).
Cédric Moonen Software developer
Charting control [v1.5] OpenGL game tutorial in C++ -
Rajesh R Subramanian wrote:
To tackle this, the memory manager marks that portion of memory as 'available for use',
I'm not an expert on the stack but I guess there's even no need to mark some memory blocks as available. It is a stack, so it means that things that are 'pushed' (e.g. memory allocated on the stack) last on the stack needs to be 'popped' (freed) first. So, simply having a pointer which points to the top of the stack is enough. That's also the reason why allocating memory on the stack is much faster: you just allocate a certain amount of memory on the top of the stack. No need to search in different memory regions to find a block which is big enough (which is the case when you allocate memory on the heap).
Cédric Moonen Software developer
Charting control [v1.5] OpenGL game tutorial in C++I rather intended explaining conceptually how the MMU works, not being specific to stack. I was wanting to explain him the usage of placement new (as in the heap) as well, so got slightly messed up. Thanks for pointing out. :)
Many are stubborn in pursuit of the path they have chosen, few in pursuit of the goal - Friedrich Nietzsche .·´¯`·->Rajesh<-·´¯`·. [Microsoft MVP - Visual C++]