当前位置: 首页 > ops >正文

reinterpret_cast and static cast

什么时候使用 reinterpret_cast?

指针类型之间的转换: 当需要将一种类型的指针转换为另一种类型的指针时,可以使用 reinterpret_cast。例如:

int* intPtr = new int(10);
void* voidPtr = reinterpret_cast<void*>(intPtr); // int* 转换为 void*
int* backToIntPtr = reinterpret_cast<int*>(voidPtr); // void* 转换回 int*

不同类型指针之间的转换: 当需要将一种类型的指针转换为完全不相关的另一种类型的指针时,例如:

int* intPtr = new int(42);
char* charPtr = reinterpret_cast<char*>(intPtr); // int* 转换为 char*

整数与指针之间的转换: 在某些底层编程场景中(如嵌入式系统或驱动开发),可能需要将整数转换为指针,或将指针转换为整数:

uintptr_t intValue = reinterpret_cast<uintptr_t>(intPtr); // 指针转换为整数
int* ptr = reinterpret_cast<int*>(intValue); // 整数转换回指针

处理类层次结构中的非多态类型: 如果类之间没有继承关系,或者不涉及虚函数(即非多态类型),可以用 reinterpret_cast 进行类型转换。例如:

struct A { int x; };
struct B { int y; };
A* a = new A{42};
B* b = reinterpret_cast<B*>(a); // A* 转换为 B*
  • 危险性:reinterpret_cast 不检查类型安全性,可能会导致未定义行为(undefined behavior)。使用时必须确保目标类型和源类型在内存布局上兼容。
  • 平台依赖性:指针大小和内存对齐规则因平台而异,因此使用 reinterpret_cast 的代码可能不可移植。
  • 仅在必要时使用:应优先考虑更安全的类型转换方式,如 static_cast 或 dynamic_cast。只有在明确需要低级别控制且了解风险时才使用 reinterpret_cast。
  • 避免在多态类中使用:如果涉及多态类(有虚函数),应使用 dynamic_cast 或 static_cast,因为 reinterpret_cast 不处理虚表或继承关系。

使用 static_cast 进行 int* 到 void* 的转换

在C++中,static_cast 允许在某些类型的指针之间进行安全的隐式转换,包括将任何类型的指针(如 int*)转换为 void*。这是因为 void* 是一种通用指针类型,标准允许从任何对象指针类型隐式转换为 void*,而 static_cast 可以显式地执行这种转换。

int* intPtr = new int(42);
void* voidPtr = static_cast<void*>(intPtr); // 合法
int* backToIntPtr = static_cast<int*>(voidPtr); // 合法,假设 voidPtr 指向 intdouble* doublePtr = new double(3.14);
void* voidPtr = static_cast<void*>(doublePtr);
int* intPtr = static_cast<int*>(voidPtr); // 编译通过,但未定义行为int* intPtr = new int(42);
char* charPtr = static_cast<char*>(intPtr); // 错误:编译失败
char* charPtr2 = reinterpret_cast<char*>(intPtr); // 合法,但需谨慎

http://www.xdnf.cn/news/17478.html

相关文章:

  • 【PyTorch】单目标检测项目
  • 深度解析1688关键字搜索API接口:技术实现与应用探索
  • crc32算法php版----crc32.php
  • 什么是ABA问题?
  • 【牛客刷题】REAL800 棋盘
  • 随想记——excel报表
  • WinForm之TreeView控件
  • Excel版经纬度和百分度互转v1.1
  • 复现论文《多无人机协同任务分配算法设计与实现》
  • 【YOLO11改进 - C3k2融合】C3k2融合EBlock(Encoder Block):低光增强编码器块,利用傅里叶信息增强图像的低光条件
  • Spring 依赖注入、AOP代理
  • Stlink识别不到-安装驱动
  • Redis基本原理,性能优化和参数调优简述
  • Lua基础+Lua数据类型
  • 正则表达式常用语法参考
  • es查询小结
  • 机械学习--DBSCAN 算法(附实战案例)
  • 本地WSL部署接入 whisper + ollama qwen3:14b 总结字幕校对增强版
  • C++基础语法积累
  • Rust进阶-part6-宏
  • LLaMA-Adapter V2 Parameter-Efficient Visual Instruction Model
  • Shell脚本-数组定义
  • Android 四大布局:使用方式与性能优化原理
  • ELK分布式日志采集系统
  • 手写 Spring
  • 《Go小技巧易错点100例》第三十七篇
  • 创建降阶模型用于搅拌槽中的涡流预测
  • 线性代数1000题学习笔记
  • 【机器学习深度学习】Embedding 模型详解:从基础原理到实际应用场景
  • NLP——TF-IDF算法