strict-aliasing

What's a proper way of type-punning a float to an int and vice-versa?

心不动则不痛 提交于 2019-11-26 10:41:57
The code below performs a fast inverse square root operation by some bit hacks. The algorithm was probably developed by Silicon Graphics in early 1990's and it's appeared in Quake 3 too. more info However I get the following warning from GCC C++ compiler : dereferencing type-punned pointer will break strict-aliasing rules Should I use static_cast , reinterpret_cast or dynamic_cast instead in such situations? float InverseSquareRoot(float x) { float xhalf = 0.5f*x; int32_t i = *(int32_t*)&x; i = 0x5f3759df - (i>>1); x = *(float*)&i; x = x*(1.5f - xhalf*x*x); return x; } Forget casts. Use memcpy

will casting around sockaddr_storage and sockaddr_in break strict aliasing

早过忘川 提交于 2019-11-26 10:29:46
问题 Following my previous question, I\'m really curious about this code - case AF_INET: { struct sockaddr_in * tmp = reinterpret_cast<struct sockaddr_in *> (&addrStruct); tmp->sin_family = AF_INET; tmp->sin_port = htons(port); inet_pton(AF_INET, addr, tmp->sin_addr); } break; Before asking this question, I\'ve searched across SO about same topic and have got mix responses about this topic. For example, see this, this and this post which say that it is somehow safe to use this kind of code. Also

gcc, strict-aliasing, and horror stories [closed]

大憨熊 提交于 2019-11-26 08:51:31
问题 In gcc-strict-aliasing-and-casting-through-a-union I asked whether anyone had encountered problems with union punning through pointers. So far, the answer seems to be No . This question is broader: Do you have any horror stories about gcc and strict-aliasing? Background: Quoting from AndreyT\'s answer in c99-strict-aliasing-rules-in-c-gcc: \"Strict aliasing rules are rooted in parts of the standard that were present in C and C++ since the beginning of [standardized] times. The clause that

Is there a (semantic) difference between the return value of placement new and the casted value of its operand?

社会主义新天地 提交于 2019-11-26 08:27:46
问题 Is there a (semantic) difference between the return value of placement new and the casted value of its operand? struct Foo { ... }; char buffer[...]; Foo *a = new(buffer) Foo; Foo *b = reinterpret_cast<Foo *>(buffer); Does a and b differ in some way? EDIT: Based on DaBler\'s comment, this question tells that there is a difference, if const/reference members used: Placement new and assignment of class with const member So, my little-bit updated question: Does a and b differ in any way, if Foo

gcc, strict-aliasing, and casting through a union

狂风中的少年 提交于 2019-11-26 08:08:41
问题 Do you have any horror stories to tell? The GCC Manual recently added a warning regarding -fstrict-aliasing and casting a pointer through a union: [...] Taking the address, casting the resulting pointer and dereferencing the result has undefined behavior [emphasis added], even if the cast uses a union type, e.g.: union a_union { int i; double d; }; int f() { double d = 3.0; return ((union a_union *)&d)->i; } Does anyone have an example to illustrate this undefined behavior? Note this question

Aliasing T* with char* is allowed. Is it also allowed the other way around?

耗尽温柔 提交于 2019-11-26 06:36:14
问题 Note: This question has been renamed and reduced to make it more focused and readable. Most of the comments refer to the old text. According to the standard, objects of different type may not share the same memory location. So this would not be legal: std::array<short, 4> shorts; int* i = reinterpret_cast<int*>(shorts.data()); // Not OK The standard, however, allows an exception to this rule: any object may be accessed through a pointer to char or unsigned char : int i = 0; char * c =

Strict aliasing rule and &#39;char *&#39; pointers

倾然丶 夕夏残阳落幕 提交于 2019-11-26 04:52:05
问题 The accepted answer to What is the strict aliasing rule? mentions that you can use char * to alias another type but not the other way. It doesn\'t make sense to me — if we have two pointers, one of type char * and another of type struct something * pointing to the same location, how is it possible that the first aliases the second but the second doesn\'t alias the first? 回答1: if we have two pointers, one of type char * and another of type struct something * pointing to the same location, how

Why does optimisation kill this function?

痞子三分冷 提交于 2019-11-26 04:20:22
We recently had a lecture in university about programming specials in several languages. The lecturer wrote down the following function: inline u64 Swap_64(u64 x) { u64 tmp; (*(u32*)&tmp) = Swap_32(*(((u32*)&x)+1)); (*(((u32*)&tmp)+1)) = Swap_32(*(u32*) &x); return tmp; } While I totally understand that this is also really bad style in terms of readability, his main point was that this part of code worked fine in production code until they enabled a high optimization level. Then, the code would just do nothing. He said that all the assignments to the variable tmp would be optimized out by the

What&#39;s a proper way of type-punning a float to an int and vice-versa?

旧街凉风 提交于 2019-11-26 03:25:29
问题 The code below performs a fast inverse square root operation by some bit hacks. The algorithm was probably developed by Silicon Graphics in early 1990\'s and it\'s appeared in Quake 3 too. more info However I get the following warning from GCC C++ compiler : dereferencing type-punned pointer will break strict-aliasing rules Should I use static_cast , reinterpret_cast or dynamic_cast instead in such situations? float InverseSquareRoot(float x) { float xhalf = 0.5f*x; int32_t i = *(int32_t*)&x;

C undefined behavior. Strict aliasing rule, or incorrect alignment?

只谈情不闲聊 提交于 2019-11-26 02:24:42
问题 I can\'t explain the execution behavior of this program: #include <string> #include <cstdlib> #include <stdio.h> typedef char u8; typedef unsigned short u16; size_t f(u8 *keyc, size_t len) { u16 *key2 = (u16 *) (keyc + 1); size_t hash = len; len = len / 2; for (size_t i = 0; i < len; ++i) hash += key2[i]; return hash; } int main() { srand(time(NULL)); size_t len; scanf(\"%lu\", &len); u8 x[len]; for (size_t i = 0; i < len; i++) x[i] = rand(); printf(\"out %lu\\n\", f(x, len)); } So, when it