strict-aliasing

Violating strict-aliasing, even without any casting?

流过昼夜 提交于 2019-12-08 04:35:56
问题 I think I'm really asking: is aliasing "transitive"? If the compiler knows that A might alias B, and B might alias C, then surely it should remember that A might therefore alias C. Perhaps this "obvious" transitive logic isn't required however? An example, for clarity. The most interesting example, to me, of a strict-aliasing issue: // g++ -fstrict-aliasing -std=c++11 -O2 #include <iostream> union { int i; short s; } u; int * i = &u.i; int main() { u.i = 1; // line 1 *i += 1; // line 2 short

About generic pointer to char and strict aliasing

两盒软妹~` 提交于 2019-12-08 02:50:17
问题 I don't know why the following code works fine, without gcc errors ( -fstrict-aliasing -Wstrict-aliasing=1 ). #include <stdio.h> int main(void) { char n = 42; char *p = &n; int *q = (int *)p; *q = 10; printf("%d|%d\n", *p, *q); return 0; } If I follow the strict aliasing rule: n1570, § 6.5 Expressions An object shall have its stored value accessed only by an lvalue expression that has one of the following types: — a type compatible with the effective type of the object, — a qualified version

What are the strict aliasing rules when casting *from* a char array?

拜拜、爱过 提交于 2019-12-07 02:24:20
问题 I'm confused by the strict aliasing rules when it comes to casting a char array to other types. I know that it is permitted to cast any object to a char array, but I'm not sure what happens the other way around. Take a look at this: #include <type_traits> using namespace std; struct{ alignas (int) char buf[sizeof(int)]; //correct? } buf1; alignas(int) char buf2[sizeof(int)]; //incorrect? struct{ float f; //obviously incorrect } buf3; typename std::aligned_storage<sizeof(int), alignof(int)>:

Is it well-defined to use memset on a dynamic bool array?

半腔热情 提交于 2019-12-06 20:59:38
问题 Is this code well-defined behavior, in terms of strict aliasing? _Bool* array = malloc(n); memset(array, 0xFF, n); _Bool x = array[0]; The rule of effective type has special cases for memcpy and memmove (C17 6.5 §6) but not for memset . My take is that the effective type becomes unsigned char . Because the second parameter of memset is required to be converted to unsigned char (C17 7.24.6.1) and because of the rule of effective type, (C17 6.5 §6): ...or is copied as an array of character type

Is this strict aliasing example correct?

隐身守侯 提交于 2019-12-06 19:25:34
问题 I've been reading up on the strict aliasing rules over the last week or so and ran into this article: Understanding C/C++ Strict Aliasing. The article goes through several ways two swap the halves of a 32-bit integer, giving both good examples and ones that violate the strict aliasing rule. I'm having trouble understanding one of the examples, though. This code is described as broken. uint32_t swaphalves(uint32_t a) { a = (a >> 16) | (a << 16); return a; } The reason given is: This version

Efficiently generating byte buffer without breaking strict aliasing

人盡茶涼 提交于 2019-12-06 09:32:01
问题 This is such a simple pattern, there has to be a "nice" way of sorting it out. I have a function that needs to generate a dynamically sized byte array containing arithmetic data. // Given that I have a function that kinda looks like this: void dispatch(std::vector<char> data); //Will take possesion of data. // The behavior I want, but this breaks strict aliasing void bad_foo(int c) { std::vector<char> real_data(c * sizeof(float)); float* raw_data = reinterpret_cast<float*>(real_data.data());

About generic pointer to char and strict aliasing

一笑奈何 提交于 2019-12-06 08:13:13
I don't know why the following code works fine, without gcc errors ( -fstrict-aliasing -Wstrict-aliasing=1 ). #include <stdio.h> int main(void) { char n = 42; char *p = &n; int *q = (int *)p; *q = 10; printf("%d|%d\n", *p, *q); return 0; } If I follow the strict aliasing rule: n1570, § 6.5 Expressions An object shall have its stored value accessed only by an lvalue expression that has one of the following types: — a type compatible with the effective type of the object, — a qualified version of a type compatible with the effective type of the object, — a type that is the signed or unsigned

C++ strict-aliasing agnostic cast

北城余情 提交于 2019-12-06 04:34:32
问题 I've read lots of QAs about strict aliasing here in Stack Overflow but all they are pretty common and discussion always tends to refer to deep-deep details of C++ standard which are almost always are difficult to understand properly. Especially when standard do not say things directly but describes something in a muddy unclear way. So, my question is probably a possible duplicate of tonns of QAs here, but, please, just answer a specific question: Is it a correct way to do a "nonalias_cast"?:

Is this use of the Effective Type rule strictly conforming?

℡╲_俬逩灬. 提交于 2019-12-06 04:29:51
The Effective Type rule in C99 and C11 provides that storage with no declared type may be written with any type and, that storing a value of a non-character type will set the Effective Type of the storage accordingly. Setting aside the fact that INT_MAX might be less than 123456789, would the following code's use of the Effective Type rule be strictly conforming? #include <stdlib.h> #include <stdio.h> /* Performs some calculations using using int, then float, then int. If both results are desired, do_test(intbuff, floatbuff, 1); For int only, do_test(intbuff, intbuff, 1); For float only, do

reinterpret_cast vs strict aliasing

半腔热情 提交于 2019-12-05 21:34:20
问题 I was reading about strict aliasing, but its still kinda foggy and I am never sure where is the line of defined / undefined behaviour. The most detailed post i found concentrates on C. So it would be nice if you could tell me if this is allowed and what has changed since C++98/11/... #include <iostream> #include <cstring> template <typename T> T transform(T t); struct my_buffer { char data[128]; unsigned pos; my_buffer() : pos(0) {} void rewind() { pos = 0; } template <typename T> void push