为什么写本文?
之所以想起来写这篇文章,是因为最近的项目代码频频报出“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》
《What is the Strict Aliasing Rule and Why do we care?》
《Understanding Strict Aliasing》
《Understanding C/C++ Strict Aliasing》
《Type-punning and the strict aliasing rule》
《ARM: Efficient C for ARM: Pointer Aliasing》
《About the C ++ aliasing, dexterous optimization and mean bugs》
《The Strict Aliasing Situation is Pretty Bad》
《Type Punning, Strict Aliasing, and Optimization》
《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》
《How to Access Safely Unaligned Data》
《Demystifying The Restrict Keyword》