strict-aliasing

Does removing const from a pointer-to-const obey strict aliasing in C, and refer to the same object?

久未见 提交于 2019-11-26 23:20:22
问题 Does the following code in C have defined behavior? int main() { const int i = 0; return *(int*)(&i); } I ask because 6.5/7 lists "a qualified version of a type compatible with the effective type of the object" as a valid alias. But the effective type of the object is const int , and I don't think int is a qualified version of const int (although the reverse is true). Neither are int and const int compatible (6.7.3/10). Furthermore, 6.3.2.3/2 says that you can convert pointer types by adding

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

。_饼干妹妹 提交于 2019-11-26 22:51:48
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 doesn't have const or reference members? Only a can safely be used to directly access the Foo object

gcc, strict-aliasing, and casting through a union

China☆狼群 提交于 2019-11-26 22:03:24
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 is not about what the C99 standard says, or does not say. It is about the actual functioning of gcc ,

strict aliasing and alignment

余生颓废 提交于 2019-11-26 19:37:06
问题 I need a safe way to alias between arbitrary POD types, conforming to ISO-C++11 explicitly considering 3.10/10 and 3.11 of n3242 or later. There are a lot of questions about strict aliasing here, most of them regarding C and not C++. I found a "solution" for C which uses unions, probably using this section union type that includes one of the aforementioned types among its elements or nonstatic data members From that I built this. #include <iostream> template <typename T, typename U> T& access

Aliasing Arrays through structs

爷,独闯天下 提交于 2019-11-26 18:26:21
问题 I'm reading paragraph 7 of 6.5 in ISO/IEC 9899:TC2. It condones lvalue access to an object through: an aggregate or union type that includes one of the aforementioned types among its members (including, recursively, a member of a subaggregate or contained union), Please refer to the document for what 'aforementioned' types are but they certainly include the effective type of the object. It is in a section noted as: The intent of this list is to specify those circumstances in which an object

Strict aliasing rule and 'char *' pointers

你。 提交于 2019-11-26 17:50:15
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? 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? It does, but that's

How to implement “_mm_storeu_epi64” without aliasing problems?

ぐ巨炮叔叔 提交于 2019-11-26 16:42:30
问题 (Note: Although this question is about "store", the "load" case has the same issues and is perfectly symmetric.) The SSE intrinsics provide an _mm_storeu_pd function with the following signature: void _mm_storeu_pd (double *p, __m128d a); So if I have vector of two doubles, and I want to store it to an array of two doubles, I can just use this intrinsic. However, my vector is not two doubles; it is two 64-bit integers, and I want to store it to an array of two 64-bit integers. That is, I want

What is the effective type of an object written by memset?

旧巷老猫 提交于 2019-11-26 15:59:37
问题 Code 1: unsigned int *p = malloc(sizeof *p); memset(p, 0x55, sizeof *p); unsigned int u = *p; Code 2: void *d = malloc(50); *(double *)d = 1.23; memset(d, 0x55, 50); unsigned int u = *(unsigned int *)d; In each case, what effect does memset have on the effective type of the object in the malloc'd space; and so is initializing u correct or a strict aliasing violation? The definition of effective type (C11 6.5/6) is: The effective type of an object for an access to its stored value is the

Does accessing the first field of a struct via a C cast violate strict aliasing?

为君一笑 提交于 2019-11-26 14:22:01
问题 Does this code violate strict aliasing? struct {int x;} a; *(int*)&a = 3 More abstractly, is it legal to cast between different types as long as the primitive read/write operations are type correct? 回答1: First, it is legal to cast in C. §6.7.2.1/13: Within a structure object, the non-bit-field members and the units in which bit-fields reside have addresses that increase in the order in which they are declared. A pointer to a structure object, suitably converted, points to its initial member

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

浪子不回头ぞ 提交于 2019-11-26 11:24:37
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 is compiled with -O3 with gcc, and run with argument 25, it raises a segfault. Without optimizations it works