strict-aliasing

Can a struct alias its own initial and only member?

风流意气都作罢 提交于 2019-12-03 11:39:42
问题 For example, is this code valid, or does it invoke undefined behavior by violating the aliasing rules? int x; struct s { int i; } y; x = 1; y = *(struct s *)&x; printf("%d\n", y.i); My interest is in using a technique based on this to develop a portable method for performing aliased reads. Update: here is the intended usage case, a little bit different, but it should be valid if and only if the above is valid: static inline uint32_t read32(const unsigned char *p) { struct a { char r[4]; };

Is this use of unions strictly conforming?

南楼画角 提交于 2019-12-03 09:36:03
问题 Given the code: struct s1 {unsigned short x;}; struct s2 {unsigned short x;}; union s1s2 { struct s1 v1; struct s2 v2; }; static int read_s1x(struct s1 *p) { return p->x; } static void write_s2x(struct s2 *p, int v) { p->x=v;} int test(union s1s2 *p1, union s1s2 *p2, union s1s2 *p3) { if (read_s1x(&p1->v1)) { unsigned short temp; temp = p3->v1.x; p3->v2.x = temp; write_s2x(&p2->v2,1234); temp = p3->v2.x; p3->v1.x = temp; } return read_s1x(&p1->v1); } int test2(int x) { union s1s2 q[2]; q->v1

Cast array of bytes to POD

丶灬走出姿态 提交于 2019-12-03 08:52:32
问题 Let's say, I have an array of unsigned chars that represents a bunch of POD objects (e.g. either read from a socket or via mmap). Which types they represent and at what position is determined at runtime, but we assume, that each is already properly aligned. What is the best way to "cast" those bytes into the respective POD type? A solution should either be compliant to the c++ standard (let's say >= c++11) or at least be guaranteed to work with g++ >= 4.9, clang++ >= 3.5 and MSVC >= 2015U3.

Reusing a float buffer for doubles without undefined behaviour

别来无恙 提交于 2019-12-03 08:22:34
问题 In one particular C++ function, I happen to have a pointer to a big buffer of floats that I want to temporarily use to store half the number of doubles. Is there a method to use this buffer as scratch space for storing the doubles, which is also allowed (i.e., not undefined behaviour) by the standard? In summary, I would like this: void f(float* buffer) { double* d = reinterpret_cast<double*>(buffer); // make use of d d[i] = 1.; // done using d as scratch, start filling the buffer buffer[j] =

Is using the result of new char[] or malloc to casted float * is UB (strict aliasing violation)?

孤人 提交于 2019-12-03 06:02:49
问题 Which code of these has UB (specifically, which violates strict aliasing rule)? void a() { std::vector<char> v(sizeof(float)); float *f = reinterpret_cast<float *>(v.data()); *f = 42; } void b() { char *a = new char[sizeof(float)]; float *f = reinterpret_cast<float *>(a); *f = 42; } void c() { char *a = new char[sizeof(float)]; float *f = new(a) float; *f = 42; } void d() { char *a = (char*)malloc(sizeof(float)); float *f = reinterpret_cast<float *>(a); *f = 42; } void e() { char *a = (char*

Is it legal to reuse memory from a fundamental type array for a different (yet still fundamental) type array

好久不见. 提交于 2019-12-03 05:18:42
This is a follow up to this other question about memory re-use. As the original question was about a specific implementation, the answer was related to that specific implementation. So I wonder whether, it is legal in a conformant implementation to re-use the memory of an array of a fundamental type for an array of a different type provided: both types are fundamental type and as such have trivial dtor and default ctor both types have same size and alignment requirement I ended with the following example code: #include <iostream> constexpr int Size = 10; void *allocate_buffer() { void * buffer

gcc: How to use __attribute((__may_alias__)) properly to avoid “derefencing type-punned pointer” warning

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-03 05:06:50
I've got some code that uses type-punning to avoid having to call a member "object"'s constructor and destructor unless/until it's actually necessary to use the object. It works fine, but under g++ 4.4.3, I get this dreaded compiler warning: jaf@jeremy-desktop:~$ g++ -O3 -Wall puns.cpp puns.cpp: In instantiation of ‘Lightweight<Heavyweight>’: puns.cpp:68: instantiated from here puns.cpp:12: warning: ignoring attributes applied to ‘Heavyweight’ after definition puns.cpp: In destructor ‘Lightweight<T>::~Lightweight() [with T = Heavyweight]’: puns.cpp:68: instantiated from here puns.cpp:20:

Does giving data an effective type count as a side-effect?

僤鯓⒐⒋嵵緔 提交于 2019-12-03 04:11:23
Suppose I have a chunk of dynamically allocated data: void* allocate (size_t n) { void* foo = malloc(n); ... return foo; } I wish to use the data pointed at by foo as a special type, type_t . But I want to do this later, and not during allocation. In order to give the allocated data an effective type , I can therefore do something like: void* allocate (size_t n) { void* foo = malloc(n); (void) *(type_t*)foo; ... return foo } As per C11 6.5/6, this lvalue access should make the effective type type_t : For all other accesses to an object having no declared type, the effective type of the object

Can a struct alias its own initial and only member?

╄→гoц情女王★ 提交于 2019-12-03 02:04:25
For example, is this code valid, or does it invoke undefined behavior by violating the aliasing rules? int x; struct s { int i; } y; x = 1; y = *(struct s *)&x; printf("%d\n", y.i); My interest is in using a technique based on this to develop a portable method for performing aliased reads. Update: here is the intended usage case, a little bit different, but it should be valid if and only if the above is valid: static inline uint32_t read32(const unsigned char *p) { struct a { char r[4]; }; union b { struct a r; uint32_t x; } tmp; tmp.r = *(struct a *)p; return tmp.x; } GCC, as desired,

Cast array of bytes to POD

只谈情不闲聊 提交于 2019-12-03 00:27:16
Let's say, I have an array of unsigned chars that represents a bunch of POD objects (e.g. either read from a socket or via mmap). Which types they represent and at what position is determined at runtime, but we assume, that each is already properly aligned. What is the best way to "cast" those bytes into the respective POD type? A solution should either be compliant to the c++ standard (let's say >= c++11) or at least be guaranteed to work with g++ >= 4.9, clang++ >= 3.5 and MSVC >= 2015U3. EDIT: On linux, windows, running on x86/x64 or 32/64-Bit arm. Ideally I'd like to do something like this