strict-aliasing Questions
4
I have a case where a friend casts a non-base class object of type "Base" to a class type object "Derived", where "Derived" is a derived class of "Base" and only adds functions, but no data. In the...
Mindszenty asked 19/6, 2013 at 14:14
1
Solved
C++ (and C) strict aliasing rules include that a char* and unsigned char* may alias any other pointer.
AFAIK there is no analogous rule for uint8_t*.
Thus my question: What are the aliasing rules...
Mitchel asked 21/4, 2017 at 20:40
2
Solved
I've noticed a heavy use of the restrict keyword in one of our legacy projects.
I understand the rationale for restrict, but I question its useful-ness when applied to some of these functions.
Ta...
Bohemian asked 10/2, 2017 at 16:42
2
Solved
Consider this code example:
#include <stdio.h>
typedef struct A A;
struct A {
int x;
int y;
};
typedef struct B B;
struct B {
int x;
int y;
int z;
};
int main()
{
B b = {1,2,3};
...
Oaxaca asked 20/2, 2017 at 19:20
3
Solved
Since C++11 std::complex<T>[n] is guaranteed to be aliasable as T[n*2], with well defined values. Which is exactly what one would expect for any mainstream architecture.
Is this guarant...
Hysell asked 23/2, 2017 at 15:38
2
Solved
Following my previous question, I'm really curious about this code -
case AF_INET:
{
struct sockaddr_in * tmp =
reinterpret_cast<struct sockaddr_in *> (&addrStruct);
tmp->sin_fam...
Gorgerin asked 11/2, 2017 at 16:19
2
Does test_func the following snippet trigger undefined behavior under the strict aliasing rules when the two arguments partially overlap?
That is the second argument is a member of the first:
#in...
Diacaustic asked 3/2, 2017 at 13:22
3
Solved
I know that the following is explicitly allowed in the standard:
int n = 0;
char *ptr = (char *) &n;
cout << *ptr;
What about this?
alignas(int) char storage[sizeof(int)];
int *ptr = ...
Blinker asked 9/8, 2016 at 23:39
4
Solved
even after reading quite a bit about the strict-aliasing rules I am still confused. As far as I have understood this, it is impossible to implement a sane memory allocator that follows these rules,...
Incoming asked 7/10, 2011 at 12:15
2
If anybody answers my question, please don't tell me to use C++.
So, I'm making a small library in C that uses an object-oriented approach. I chose to use the less-common of the two main approache...
Brookins asked 16/1, 2015 at 9:35
5
Solved
Reinterpret struct with members of the same type as an array in a standard compliant way [duplicate]
In various 3d math codebases I sometimes encounter something like this:
struct vec {
float x, y, z;
float& operator[](std::size_t i)
{
assert(i < 3);
return (&x)[i];
}
};
...
Methane asked 1/1, 2017 at 21:24
1
Solved
Consider these two functions:
int f1()
{
alignas(int) char buf[sizeof(int)] = {};
return *reinterpret_cast<int*>(buf);
}
int f2()
{
alignas(int) char buf[sizeof(int)] = {};
char* ptr = ...
Orvalorvan asked 23/12, 2016 at 9:25
2
Solved
Context
“Strict aliasing”, named after the GCC optimization, is an assumption by the compiler that a value in memory will not be accessed through an lvalue of a type (the “declared type”) very dif...
Falco asked 15/11, 2016 at 10:4
1
Solved
A char * (and qualified variants) may alias anything.
Are signed char * and unsigned char * (and their qualified variants)
exempt from this?
In other words, I've learned it's a good idea to apply ...
Enshrine asked 13/11, 2016 at 14:49
6
Solved
The general answer when asking "how does one implement memcpy function conformant with strict aliasing rules" is something along the lines of
void *memcpy(void *dest, const void *src, size_t n)
{
...
Potful asked 31/7, 2014 at 13:57
3
Solved
I would like to write a generic function to detect if an array of pointers to some arbitrary type contains a NULL. My initial attempt was something along these lines:
bool find_null (void *ptrs, s...
Demagoguery asked 10/10, 2016 at 20:54
7
Solved
How can *i and u.i print different numbers in this code, even though i is defined as int *i = &u.i;? I can only assuming that I'm triggering UB here, but I can't see how exactly.
(ideone demo ...
Plankton asked 28/9, 2016 at 21:6
2
Solved
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 "ob...
Parallax asked 28/9, 2016 at 20:40
5
My initial problem is that I have, on a project, several objects which share a lifetime (i.e., once I free one of them, I'll free them all), then I wanted to allocate a single block of memory. I ha...
Knar asked 22/9, 2016 at 13:35
2
According to N1570 6.5/6:
If a value is copied into an object having no declared type using memcpy or memmove, or is copied as an array of character type, then the effective type
of the modifie...
Airworthy asked 1/12, 2015 at 1:34
2
The majority of microcomputer C compilers have two signed integer types with the same size and representation, along with two such unsigned types. If int is 16 bits, its representation will general...
Washington asked 8/9, 2016 at 16:59
2
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 &l...
Byssinosis asked 2/9, 2016 at 14:12
6
Solved
In gcc-strict-aliasing-and-casting-through-a-union I asked whether anyone had encountered problems with union punning through pointers. So far, the answer seems to be No.
This question is br...
Miss asked 2/6, 2010 at 14:27
3
Solved
I have a function that takes a unsigned long* and needs to pass it to a external library that takes a unsigned int* and on this platform unsigned int/long are the same size.
void UpdateVar(unsigne...
Chippewa asked 21/9, 2010 at 19:40
5
The Problem
The restrict keyword in C is missing in C++, so out of interest I was looking for a way to emulate the same feature in C++.
Specifically, I would like the following to be equivalent:...
Zsolway asked 18/6, 2016 at 20:16
© 2022 - 2024 — McMap. All rights reserved.