c++ - 我可以在哪种基本数据类型中存储地址
问题描述
我想将指针存储到基本数据类型中。
我的直觉说,正确的数据类型是 astd::size_t
并且它有效:
#include <fmt/core.h>
int main() {
int i = 0;
int* p = &i;
auto const t = reinterpret_cast<std::size_t>(p);
fmt::print("size of address: {}\n", sizeof(p));
fmt::print("size of std::size_t: {}\n", sizeof(t));
fmt::print("value of address to i: {}\n", static_cast<void*>(p));
fmt::print("value of std::size_t t: {0:#x}\n", t);
return *p;
}
程序输出:
Program returned: 0
size of address: 8
size of std::size_t: 8
value of address to i: 0x7ffdd754a0dc
value of std::size_t t: 0x7ffdd754a0dc
但对于每个平台都是如此吗?
我怎么能确定?
解决方案
推荐阅读
- jquery - 如何在 JQuery 函数之外获取“this”var?
- amazon-web-services - 使用 go 按标签列出 AWS 中的负载均衡器
- shopify - 如何使用ajax从加载的产品页面上的产品中获取产品ID,并在shopify中获取相关的产品ID
- javascript - 多次链接到同一个谷歌文档时如何只打开1个新标签?
- react-native - 当我尝试设置状态时,开玩笑测试失败
- python - 以自定义形式创建新用户时违反非空约束
- angular - 如何在角度 5 中禁用 owl-date-time 选择器中的过去日期
- ios - CLLocationManager 有时会返回 (0, 0) 坐标
- angular - ReferenceError: 窗口未定义,angular6 通用
- python - django-axes 失败的登录尝试不是连续的