C语言中不同类型的指针互相转换的注意点
2016-04-23 10:24
1031 查看
为什么写本文?
之所以想起来写这篇文章,是因为最近的项目代码频频报出“break strict-aliasing rules”相关的warnning信息,追其原因就是因为不同类型的指针之间的强转引起的。我相信大家平日或多或少写过本文出现的代码,甚至有时候会因这样的trick而感到兴奋,因为很多情况下,这样的代码运行结果是没有问题的。
本文就不同类型的指针之间的转化做个比较详实的阐述:什么情况下,它们之间可以转化而不会引起问题,而又在什么情况下,强转会发生意想不到的结果呢? 关于这一切标准又是怎么说的呢? 这都是本文需要搞清楚的问题。
参考连接:
《Why is this implicit conversion (between different pointer types) valid?》
《Casting one struct pointer to other - C》
《Converting a pointer to different type in C》
《Is This “Type Punning” Well-Defined?》
《Pointers in C, Part III: The Strict Aliasing Rule》
《Understanding Strict Aliasing》
《Understanding C/C++ Strict Aliasing》
《Strict aliasing in C》
《strict aliasing》
《Type-punning and the strict aliasing rule》
《ARM: Efficient C for ARM: Pointer Aliasing》
《GCC, strict aliasing, C99》
《GCC strict aliasing》
《About the C ++ aliasing, dexterous optimization and mean bugs》
《strict aliasing》
《Strict aliasing...》
《Strict Aliasing,神坑?》
《C/C++ Strict Alias 小记》
《The Strict Aliasing Situation is Pretty Bad》
《Type Punning, Strict Aliasing, and Optimization》
《gcc strict-aliasing是什麼?》
《GCC:strict-aliasing rules》
《Restricting what you can do》
《Strict aliasing and std::vector》
《Solving the gcc 4.4 strict aliasing problems》
《C pointer aliasing violiations and aggressive compiler optimizations》
《Type punning, aliasing, unions, strict-aliasing, oh my!》
《C99 Strict Aliasing Rules and Packed Streams》
《What is the strict aliasing rule?》
《Solving the gcc 4.4 strict aliasing problems》
《Type-punning and the strict aliasing rule》
《GCC strict aliasing pitfall》
《How to Access Safely Unaligned Data》
《Demystifying The Restrict Keyword》
《RESTRICT, Keyword for Low-Level Pointer Efficiency》
《The restrict keyword in C programming》
之所以想起来写这篇文章,是因为最近的项目代码频频报出“break strict-aliasing rules”相关的warnning信息,追其原因就是因为不同类型的指针之间的强转引起的。我相信大家平日或多或少写过本文出现的代码,甚至有时候会因这样的trick而感到兴奋,因为很多情况下,这样的代码运行结果是没有问题的。
本文就不同类型的指针之间的转化做个比较详实的阐述:什么情况下,它们之间可以转化而不会引起问题,而又在什么情况下,强转会发生意想不到的结果呢? 关于这一切标准又是怎么说的呢? 这都是本文需要搞清楚的问题。
参考连接:
《Why is this implicit conversion (between different pointer types) valid?》
《Casting one struct pointer to other - C》
《Converting a pointer to different type in C》
《Is This “Type Punning” Well-Defined?》
《Pointers in C, Part III: The Strict Aliasing Rule》
《Understanding Strict Aliasing》
《Understanding C/C++ Strict Aliasing》
《Strict aliasing in C》
《strict aliasing》
《Type-punning and the strict aliasing rule》
《ARM: Efficient C for ARM: Pointer Aliasing》
《GCC, strict aliasing, C99》
《GCC strict aliasing》
《About the C ++ aliasing, dexterous optimization and mean bugs》
《strict aliasing》
《Strict aliasing...》
《Strict Aliasing,神坑?》
《C/C++ Strict Alias 小记》
《The Strict Aliasing Situation is Pretty Bad》
《Type Punning, Strict Aliasing, and Optimization》
《gcc strict-aliasing是什麼?》
《GCC:strict-aliasing rules》
《Restricting what you can do》
《Strict aliasing and std::vector》
《Solving the gcc 4.4 strict aliasing problems》
《C pointer aliasing violiations and aggressive compiler optimizations》
《Type punning, aliasing, unions, strict-aliasing, oh my!》
《C99 Strict Aliasing Rules and Packed Streams》
《What is the strict aliasing rule?》
《Solving the gcc 4.4 strict aliasing problems》
《Type-punning and the strict aliasing rule》
《GCC strict aliasing pitfall》
《How to Access Safely Unaligned Data》
《Demystifying The Restrict Keyword》
《RESTRICT, Keyword for Low-Level Pointer Efficiency》
《The restrict keyword in C programming》
相关文章推荐
- c++实现二叉树(递归)
- c++ 读书目录
- C语言基础及数据类型
- c语言中,scanf里面要是数组要不要加&?
- C++ 复杂链表的复制
- C# 调用C++工程生成的DLL
- 很好的c++和Python混合编程文章
- C++ 线程操作
- 使用C/C++扩展Python
- 设计模式 - 观察者模式(Observe pattern)C++实现
- c++中级 STL基础学习(二)
- 【UNREAL ENGINE 游戏开发】开篇之UE4的BLUEPRINT(蓝图)与C++(新童鞋必看)
- 【UNREAL ENGINE 游戏开发】开篇之UE4的BLUEPRINT(蓝图)与C++(新童鞋必看)
- C++进阶 面向对象基础(三)
- c++作业4
- LEX & FLEX 实现C语言词法分析器
- 【C++】JSON文件的读取和生成
- C++作业4
- 【代码】C++实现二叉树基本操作及测试用例
- c++的重载、覆盖和隐藏