strict-aliasing Questions
2
Solved
Assuming alignment is a uintptr_t power of 2, looking for the next properly aligned address can be done using this expression:
(address + alignment - 1u) & ~(alignment - 1u)
This is used in cu...
Earthwork asked 23/10 at 23:36
1
Solved
Recently we switched to a more recent GCC and it optimized away a whole function and replaced it with "null pointer access" trap code instead when optimizing for size. Looking at godbolt,...
Schnook asked 5/8 at 14:3
3
Solved
Now we all sometimes have to work with binary data. In C++ we work with sequences of bytes, and since the beginning char was the our building block. Defined to have sizeof of 1, it is the byte. And...
Alter asked 28/4, 2013 at 5:46
6
One of the examples of undefined behavior from the C standard reads (J.2):
— An array subscript is out of range, even if an object is apparently accessible with the
given subscript (as in the l...
Segura asked 22/9, 2010 at 3:15
3
Solved
I read that char *- and their signed and unsigned counterparts - can alias any type without violating the strict aliasing rule. However, having a char * point to an int variable and casting that ch...
Easterling asked 10/12, 2023 at 12:32
1
This question refers to the current C++20 draft. The quoted passages have been slightly modified from previous standard iterations, but not in relevant ways as far as I know.
I am looking for clar...
Vyse asked 27/2, 2020 at 17:56
1
Solved
Consider the following valarray-like class:
#include <stdlib.h>
struct va
{
void add1(const va& other);
void add2(const va& other);
size_t* data;
size_t size;
};
void va::add1(...
Darbies asked 17/8, 2023 at 10:58
2
Solved
std::byte is defined in C++17 as:
enum class byte : unsigned char {};
I'm currently stuck at using C++14, and I wonder if I add the same definition in C++14 (in some non-std namespace, along with ...
Offshore asked 9/8, 2023 at 9:17
2
Solved
After P0593R6 ('Implicit creation of objects for low-level object manipulation') was accepted in C++20, C++23 will get std::start_lifetime_as() which 'completes the functionality proposed in [P0593...
Shadshadberry asked 10/6, 2023 at 10:43
3
I have my custom little OOP-esque inheritance functionality, something like this:
// base class
struct BaseTag;
typedef struct {
int (*DoAwesomeStuff)(struct BaseTag* pInstance);
} S_BaseVtable;
...
Ietta asked 31/3, 2023 at 20:51
2
Solved
There are a few questions and answers on the site already concerning pointer-interconvertibility of structs and their first member variable, as well as structs and their first public base. This que...
Thirtythree asked 5/2, 2023 at 14:17
3
There seems to be some agreement that you can't willy nilly point (an int*) into a char array because of the C++ aliasing rules.
From this other question -- Generic char[] based storage and avoidi...
Princely asked 12/1, 2017 at 23:8
2
Solved
This example is copied from cppreference.
struct Y { int z; };
alignas(Y) std::byte s[sizeof(Y)];
Y* q = new(&s) Y{2};
const int f = reinterpret_cast<Y*>(&s)->z; // Class member ac...
Tilla asked 5/12, 2022 at 9:12
2
Solved
Can I use a std::array<int, N> to alias parts of a int[] without invoking UB?
https://en.cppreference.com/w/cpp/container/array
"This container is an aggregate type with the same semanti...
Dasha asked 19/12, 2022 at 21:16
2
So I was re-reading C17 6.5/6 - 6.5/7 regarding effective type and strict aliasing, but couldn't figure out how to treat qualifiers. Some things confuse me:
I always assumed that qualifiers aren't...
Laurellaurella asked 18/12, 2020 at 12:13
3
Solved
Given type definitions
struct a { int a; };
struct b { int b; struct a ba;};
and a function taking struct a* a and struct b* b, the type information expresses possible overlap between a->a and ...
Bun asked 27/11, 2022 at 14:26
3
Solved
Reading https://en.cppreference.com/w/cpp/language/reinterpret_cast I wonder what are use-cases of reinterpret_cast that are not UB and are used in practice?
The above description contains many cas...
Zionism asked 27/10, 2022 at 5:10
5
Consider the following two snippets:
#define ALIGN_BYTES 32
#define ASSUME_ALIGNED(x) x = __builtin_assume_aligned(x, ALIGN_BYTES)
void fn0(const float *restrict a0, const float *restrict a1,
fl...
Leveloff asked 25/3, 2013 at 11:21
4
Solved
While looking at the code for Dear Imgui, I found the following code (edited for relevance):
struct ImVec2
{
float x, y;
float& operator[] (size_t idx) { return (&x)[idx]; }
};
It's pret...
Melvinamelvyn asked 30/9, 2022 at 5:48
2
Solved
In C, what exactly are the performance benefits that come with observing strict aliasing?
Demetri asked 16/4, 2009 at 6:11
4
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...
Shammy asked 24/5, 2014 at 18:8
3
Since strict aliasing may help compiler optimize better, C99 introduced the restrict keyword which can be used as a qualifier of a variable if programmers guarantee that it won't be accessed throug...
Adust asked 7/9, 2016 at 15:29
3
I believe 6.5p7 in the C standard defines the so-called strict aliasing rule as follows.
An object shall have its stored value accessed only by an lvalue expression that has one of
the following t...
Wizardly asked 25/5, 2022 at 17:35
0
I'm very confused by the LLVM AliasAnalysis implementation.
Say I have this program:
int* key = malloc(4);
*key = 10;
*key = 11;
It gets transformed to IR code like this:
%3 = call noalias i8* @m...
Interplay asked 23/5, 2022 at 21:56
8
Solved
I'm trying to fix two warnings when compiling a specific program using GCC. The warnings are:
warning: dereferencing type-punned pointer will break
strict-aliasing rules [-Wstrict-aliasing]
and t...
Beograd asked 11/1, 2012 at 18:28
1 Next >
© 2022 - 2024 — McMap. All rights reserved.