变量作用域和指针 [英] Variable scoping and pointers

查看:84
本文介绍了变量作用域和指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习C ++.我坚持下面的代码.

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 />}

ChangeData方法中,我正在将局部变量的地址分配给所提供的参数.当ChangeData返回主目录时,我相信变量ab将被从堆栈中删除.运行上面的应用程序时遇到错误,但是上面的一个起作用了!

我很困惑这是怎么回事?如果ab从堆栈中删除了,指针将占据哪个地址以及如何获取正确的值?

解决方案

变量被破坏,但内容未被破坏. br>您知道修道院的住所,您可以继续检查它的价值...但是,您很难过,这是错误的,所以不要这样做.在该变量被销毁后的任何时候,系统都可以使用该变量的相同位置,因此,争用内容将很快失效.


Christian Flutcher写道:

我很困惑这是怎么回事? /blockquote>

因为您很幸运(或者在您的情况下不是:)).在ChangeData函数中,本地变量具有一个特定的地址,该地址存储在ptr变量中.当函数退出时,您的ptr变量仍然拥有相同的地址,但是正如您所说的,该变量已从堆栈中删除".但是删除只是意味着该内存可以用于其他目的,在您的情况下您不能这样做,因此该内存不会被覆盖(并且仍包含相同的值).如果现在您在打印值之前会做其他事情,则很有可能它不再起作用.


Christian Flutcher写道:

如果从堆栈中删除了ab,则该地址指针是否固定以及如何获取正确的值?



实际上,它不是已删除".该特定的内存位置被标记为可用".但是,直到使用它,旧值才存在. :)


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?

解决方案

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.


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.


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


这篇关于变量作用域和指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆