小编Mai*_*k R的帖子

变量的地址需要加载到内存中吗?

我有一个问题 - 使用以下代码我试图找出存储在某个地址的内容以及我的静态变量存储在这个特定位置的时间.(我读到静态变量无限存储并且非常惊讶 - 想测试这是否属实).代码定义了一个静态变量(它在我的系统上的地址是0x1000020c0 - 这可能是相当随机的但是一直是这种情况)

如果我现在想要找出该地址存储的整数值,我必须先用$ number打印出地址,然后给出0x1000020c0.重新/重新解释地址(0x1000020c0)仅提供100!如果地址是在之前打印的,或者我在重新解释/重铸中使用&编号.

有人可以解释为什么会这样吗?

int static number = 100;  

//  std::cout << number << std::endl; <- prints 100

//  prints 100 and the address 0x1000020c0 in my case
//  std::cout << number << " " << &number << std::endl;

//  this does not work unless &number is printed previously
//  std::cout << "Value is :  " << *reinterpret_cast<int*>(0x1000020c0) << std::endl;

// this does work and show the correct value (100)
std::cout << "Value is …
Run Code Online (Sandbox Code Playgroud)

c++ variables pointers

13
推荐指数
1
解决办法
1259
查看次数

标签 统计

c++ ×1

pointers ×1

variables ×1