strict-aliasing Questions
2
A colleague of mine is working on C++ code that works with binary data arrays a lot. In certain places, he has code like
char *bytes = ...
T *p = (T*) bytes;
T v = p[i]; // UB
Here, T can be som...
Moppet asked 15/6, 2018 at 6:59
1
Solved
Does the getValue() member function below violate the c++ strict aliasing rule?
According to the standard, I believe setValue() violates the strict aliasing rule since double is neither an aggrega...
Xanthochroism asked 17/5, 2018 at 3:2
2
Solved
AFAIK, there are three situations where aliasing is ok
Types that only differ by qualifier or sign can alias each other.
struct or union types can alias types contained inside them.
casting T* to...
Hankhanke asked 15/3, 2018 at 11:49
2
I've been trying to work out how legal the below is and I could really use some help.
#include <stdio.h>
#include <stdlib.h>
typedef struct foo {
int foo;
int bar;
} foo;
void make...
Whiteside asked 13/2, 2018 at 17:47
9
Solved
As previously established, a union of the form
union some_union {
type_a member_a;
type_b member_b;
...
};
with n members comprises n + 1 objects in overlapping storage: One object for the un...
Hunt asked 5/8, 2016 at 21:42
1
Solved
A common pattern when providing a C API is to forward declare some opaque types in your public header which are passed to your API methods and then reinterpret_cast them into your defined C++ types...
Cursed asked 12/3, 2018 at 11:25
3
Solved
When allocating memory for a variable sized array, I often do something like this:
struct array {
long length;
int *mem;
};
struct array *alloc_array( long length)
{
struct array *arr = malloc...
Candlestick asked 9/2, 2018 at 21:24
1
Solved
I recently read some code that had an atomic and a character in the same union. Something like this
union U {
std::atomic<char> atomic;
char character;
};
I am not entirely sure of the r...
Toname asked 8/2, 2018 at 5:43
2
Solved
Consider the following scenario:
std::array<int, 8> a;
auto p = reinterpret_cast<int(*)[8]>(a.data());
(*p)[0] = 42;
Is this undefined behavior? I think it is.
a.data() returns a i...
Paleogeography asked 25/1, 2018 at 13:28
2
Solved
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 = reinterpre...
Pangolin asked 5/12, 2017 at 12:9
2
Solved
The following example comes from std::aligned_storage page of cppreference.com:
#include <iostream>
#include <type_traits>
#include <string>
template<class T, std::size...
Unific asked 10/12, 2017 at 3:44
1
Solved
I have a struct X which inherits from struct Base. However, in my current setup, due to alignment, size of X is 24B:
typedef struct {
double_t a;
int8_t b;
} Base;
typedef struct {
Base base;
...
Blueness asked 8/12, 2017 at 8:45
4
Solved
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_s...
Pincince asked 13/9, 2017 at 19:49
3
Solved
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 = 4...
Bellamy asked 26/10, 2017 at 17:51
1
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 ...
Photometer asked 26/10, 2017 at 17:5
2
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 ...
Anaemic asked 5/10, 2017 at 18:4
3
Solved
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 a...
Irrational asked 9/10, 2017 at 10:30
3
Solved
Consider the following snippet as an example:
*pInt = 0xFFFF;
*pFloat = 5.0;
Since they are int and float pointers, the compiler will assume they don't alias and can exchange them for example.
...
Erect asked 8/9, 2017 at 12:47
4
Solved
I'm using Beej's Guide to Networking and came across an aliasing issue. He proposes a function to return either the IPv4 or IPv6 address of a particular struct:
1 void *get_in_addr( struct sockadd...
Baggett asked 15/9, 2009 at 21:10
2
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 ru...
Exaction asked 7/12, 2016 at 11:30
1
Solved
It is my understanding that something like this is okay:
const int ci = 42;
const int *cip = &ci;
int *ip = (int *)cip;
int j = *ip;
What about this?
const int ci = 42;
const int *cip = &am...
Pittsburgh asked 16/7, 2017 at 1:8
4
Solved
I have been reading about the strict aliasing rule for a while, and I'm starting to get really confused. First of all, I have read these questions and some answers:
strict-aliasing-rule-and-char-...
Herbert asked 8/6, 2017 at 22:41
2
Undefined behavior on reading object using non-character type when last written using character type
Assuming unsigned int has no trap representations, do either or both of the statements marked (A) and (B) below provoke undefined behavior, why or why not, and (especially if you think one of them ...
Nigrosine asked 21/6, 2015 at 17:41
1
Solved
Consider the following program:
#include <iostream>
#include <cmath>
#include <cstring>
#include <xmmintrin.h>
using namespace std;
int main()
{
// 4 float32s.
__m128 n...
Coenobite asked 23/5, 2017 at 11:51
7
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...
Blocky asked 25/5, 2010 at 16:6
© 2022 - 2024 — McMap. All rights reserved.