strict-aliasing

Why I get a “type-punned” warning even when using a `char *`?

有些话、适合烂在心里 提交于 2019-12-11 09:28:17
问题 gcc (6.3.1 20170109) when compiling the following program #include <stdio.h> int main(int argc, const char *argv[]) { unsigned char x[] = {0x66, 0x19}; printf("%i\n", ((short *)((char *)&x[0]))[0]); return 0; } generates as warning: pun.c: In function ‘main’: pun.c:5:5: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing] Shouldn't type aliasing allowed when using char pointers? 回答1: Here’s what C11 (or at least the free draft N1570) has to say about

Do the strict aliasing rules in C++20 allow `reinterpret_cast` between the standard c++ unicode chars and the underlining types?

巧了我就是萌 提交于 2019-12-11 04:08:57
问题 Do the C++20 's strict aliasing rules [basic.lval]/11 arbitrarily allow following... cast between char* and char8_t* string str = "string"; u8string u8str { (char8_t*) &*str.data() }; // c++20 u8string u8string u8str2 = u8"zß水🍌" string str2 { (char*) u8str2.data() }; cast between uint32_t* , uint_least32_t* and char32_t* vector<uint32_t> ui32vec = { 0x007a, 0x00df, 0x6c34, 0x0001f34c }; u32string u32str { (char32_t*) &*ui32vec.data(), ui32vec.size() }; u32string u32str2 = U"zß水🍌" vector

FFTW : how to prevent breaking aliasing rules?

若如初见. 提交于 2019-12-10 20:43:46
问题 I have a code which uses the std::complex<double> type. From FFTW Manual : if you have a variable complex<double> *x , you can pass it directly to FFTW via reinterpret_cast<fftw_complex*>(x) . However, when I do this in my code : tmp_spectrum = reinterpret_cast<std::complex<double>*>(fftw_alloc_complex(conf.spectrumSize())); plan_bw_temp = fftw_plan_dft_c2r_1d(conf.FFTSize(), reinterpret_cast<fftw_complex*>(tmp_spectrum), tmp_out, FFTW_ESTIMATE); I get dereferencing type-punned pointer might

MSVC++ restrict keyword and local variables

假装没事ソ 提交于 2019-12-10 17:46:40
问题 I've read a number of posts on the restrict keyword. But virtually every example I can find seem to refer to input parameters only to a function and, perhaps a single value. I need to clarify my understanding. I've found a function that looks like it totally violates the rules of the keyword with both an input parameter and a local variable. This function is called with a void* to a buffer and the pointer is declared as _restrict (this is Microsoft Visual C++). Yet later in the function, a

Is strict aliasing is c or c++ thing?

两盒软妹~` 提交于 2019-12-10 13:32:33
问题 In ISO/IEC 9899:TC2, the standard says following 6.3.2.3 Pointers A pointer to an object or incomplete type may be converted to a pointer to a different object or incomplete type. If the resulting pointer is not correctly aligned for the pointed-to type, the behavior is undefined. Otherwise, when converted back again, the result shall compare equal to the original pointer. When a pointer to an object is converted to a pointer to a character type, the result points to the lowest addressed byte

Unions and strict aliasing in C11

旧时模样 提交于 2019-12-10 13:29:57
问题 Assuming I have a union like this union buffer { struct { T* data; int count; int capacity; }; struct { void* data; int count; int capacity; } __type_erased; }; Will I get into trouble if I mix reads/writes to the anonymous struct members and __type_erased members under C11 aliasing rules? More specifically, I am interested in the behaviour that occurs if the components are accessed independently (e.g. via different pointers). To illustrate: grow_buffer(&buffer.__type_erased); buffer.data

Strict pointer aliasing: any solution for a specific problem?

别来无恙 提交于 2019-12-10 13:00:58
问题 I have a problem caused by breaking strict pointer aliasing rule. I have a type T that comes from a template and some integral type Int of the same size (as with sizeof ). My code essentially does the following: T x = some_other_t; if (*reinterpret_cast <Int*> (&x) == 0) ... Because T is some arbitary (other than the size restriction) type that could have a constructor, I cannot make a union of T and Int . (This is allowed only in C++0x only and isn't even supported by GCC yet). Is there any

Buffer filled with different types of data, and strict aliasing

≡放荡痞女 提交于 2019-12-10 02:12:02
问题 According to the standard, it is always undefined behavior in C++ to make, for example, a float* point to the same memory location as a int* , and then read/write from them. In the application I have, there can be a buffer filled with 32-bit integer elements, that are overwritten by 32-bit floating point elements. (It actually contains a representation of an image, that gets transformed in multiple stages by GPU kernels, but there should also be a host implementation that does the same

What is the no-undefined-behavior way of deserializing an object from a byte array in C++11 (or later)?

我是研究僧i 提交于 2019-12-10 01:43:53
问题 To overcome alignment issues, I need to memcpy into a temporary. What type should that temporary be? gcc complains that the following reinterpret_cast will break strict aliasing rules: template <typename T> T deserialize(char *ptr) { static_assert(std::is_trivially_copyable<T>::value, "must be trivially copyable"); alignas(T) char raw[sizeof(T)]; memcpy(raw, ptr, sizeof(T)); return *reinterpret_cast<T *>(raw); } (e.g. when T is "long"). I don't want to define a T, since I don't want to

_Bool type and strict aliasing

匆匆过客 提交于 2019-12-08 18:53:52
问题 I was trying to write some macros for type safe use of _Bool and then stress test my code. For evil testing purposes, I came up with this dirty hack: _Bool b=0; *(unsigned char*)&b = 42; Given that _Bool is 1 byte on the implementation sizeof(_Bool)==1 ), I don't see how this hack violates the C standard. It shouldn't be a strict aliasing violation. Yet when running this program through various compilers, I get problems: #include <stdio.h> int main(void) { _Static_assert(sizeof(_Bool)==1, "