如何使用内存地址打印变量的值
How to print value of a Variable using its memory address?
假设0xfe2200
是变量var2
的内存地址,我想显示其中存储的值,例如
cout<< "Value stored in the given address is : " << 0xfe2200 << " ";
我试着跟随,但都是徒劳的
cout << "Value is : " << *0xfee2200 << " ;
cout << "Value is : " << &0xfee200 << " ;
假设地址指向int
,您可以:
cout << "Value is : " << *reinterpret_cast<int*>(0xfee2200);
作为文字0xfee2200
是整数类型,而您期望的是指针。
你必须决定作为什么类型的数据你想要解释内存内容和转换它相应:
const char* tmp = "foofoo"; // Valid ptr for this example
const void* address = tmp; // Set to your address
const int* i = reinterpret_cast<const int*>(address);
const unsigned short* us = reinterpret_cast<const unsigned short*>(address);
const char* c = reinterpret_cast<const char*>(address);
std::cout << "i: " << (*i)
<< "nus: " << (*us)
<< "nc: " << (*c);
输出:我:1718579046美国:28518
C: f
相关文章:
- 打印所有继承的类成员变量和方法
- 为什么我的字符串变量没有打印完整的字符串?
- 从模板创建通用打印函数,以打印基元数据类型变量的值
- 在我的变量打印C++后无法获得 $ 打印符号
- 为自定义打印调试实现传递任何类型的变量
- std::cout 在打印变量与函数表达式时的行为不同
- 打印类中的变量
- 如何在循环中使用scanf,将值存储到一个变量中,然后打印出来?
- 为什么变量的打印地址在每次执行时都会打印随机值,即使它是 C 中的逻辑地址?
- 使用更改此变量的函数在同一行中打印引用变量
- 如何让m_refcount变量打印出我想要的值而不是垃圾
- 面临在 if 语句之外打印变量数据的问题 完成使用 Qt 编程
- 错误:变量或字段'PrintEntity'声明无效无效打印实体(实体 e);
- 使用指针变量打印特定的数组变量
- 使用自动变量打印矢量的内容
- void 函数使用相同的变量打印两个不同的答案
- 为什么字符串变量打印与垃圾
- cout<<变量;打印出空白
- 为什么未初始化的变量打印一个奇怪的负值
- 当一个变量打印未初始化的变量contain时,在C++中该怎么办