Questions tagged [pointer-aliasing]
59 questions
27
votes
4 answers
How to cast sockaddr_storage and avoid breaking strict-aliasing rules
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 sockaddr *sa )
2 {
3 if (sa->sa_family == AF_INET)
4…

sinoth
- 7,602
- 3
- 19
- 22
20
votes
5 answers
Does this code subvert the C++ type system?
I understand that having a const method in C++ means that an object is read-only through that method, but that it may still change otherwise.
However, this code apparently changes an object through a const reference (i.e. through a const method).
Is…

user541686
- 205,094
- 128
- 528
- 886
17
votes
4 answers
Nested structs and strict aliasing in c
Please consider the following code:
typedef struct {
int type;
} object_t;
typedef struct {
object_t object;
int age;
} person_t;
int age(object_t *object) {
if (object->type == PERSON) {
return ((person_t *)object)->age;
} else {
…

Johannes
- 871
- 1
- 10
- 16
16
votes
1 answer
Array pointer aliasing - undefined behavior?
Does the following code invoke undefined behavior (due to aliasing violation or otherwise)?
int foo(int (*a)[10], int (*b)[5])
{
(*a)[5]++;
return (*b)[0];
}
int x[10];
foo(&x, (int (*)[5])&x[5]);
Note that the corresponding code using…

R.. GitHub STOP HELPING ICE
- 208,859
- 35
- 376
- 711
11
votes
5 answers
What is the "right" way to avoid Aliasing (e.g. when adding an element of a container to itself) in C++?
std::vector a;
a.push_back(1);
a.push_back(a[0]);
I just learned that the code above can be very dangerous.
(If it's not obvious why, you're not alone... it wasn't obvious to me either.)
My questions:
What is the "standard" way of dealing…

user541686
- 205,094
- 128
- 528
- 886
10
votes
1 answer
gcc C/C++ assume no pointer aliasing
Having recently read that the main reason why fortran is faster than c/c++ in numerical computations is because there is no pointer aliasing.
Apparently, using restrict or __restrict__ keywords allows on a case by case basis to indicate the absence…

vkubicki
- 1,104
- 1
- 11
- 26
10
votes
2 answers
Is there anything like a restrict keyword for C++ to indicate that _iterators_ are not aliased
g++ does implement __restrict__ for pointers, but I could not find anything about iterators. My overall intent is to encourage the compiler to vectorize stl loops.
Edit:
Even if the compiler is unable to vectorize, the __restrict__ keyword should be…

srean
- 2,578
- 18
- 18
8
votes
4 answers
How to tell a C or a C++ compiler that pointers are not aliased
I have function that receives an array of pointers like so:
void foo(int *ptrs[], int num, int size)
{
/* The body is an example only */
for (int i = 0; i < size; ++i) {
for (int j = 0; j < num-1; ++j)
ptrs[num-1][i] +=…

san
- 4,144
- 6
- 32
- 50
6
votes
4 answers
Prevent two object internals from aliasing
I have a function signature similiar to this
void Mutliply(const MatrixMN& a, const MatrixMN& b, MatrixMN& out);
Internally the matrix class has a float* data; that represents the m x n components. I'd like to tell the compiler that a and b do not…

coderdave
- 895
- 2
- 6
- 15
6
votes
2 answers
Is aliasing of mutable references correct in unsafe code?
In unsafe code, is it correct to have several mutable references (not pointers) to the same array, as long as they are not used to write to the same indices?
Context
I would like to yield several (distinct) mutable views of an underlying array, that…

rom1v
- 2,752
- 3
- 21
- 47
6
votes
4 answers
Placement-new vs gcc 4.4.3 strict-aliasing rules
I've got some code that I've been using successfully for some years to implement a "variant-type object"; that is, a C++ object that can hold a values of various types, but only uses (approximately) as much memory as the largest of the possible…

Jeremy Friesner
- 70,199
- 15
- 131
- 234
6
votes
3 answers
Consequenes of warning “dereferencing type-punned pointer will break strict-aliasing rules”
I have gone through some queries on the similar topic and some material related to it.
But my query is mainly to understand the warning for the below code. I do not want a fix !!
I understand there are two ways, a union or using memcpy.
uint32…

DarkKnight
- 131
- 10
6
votes
3 answers
Is it legal to alias "const restrict" pointer arguments?
If dot_product is declared as
float dot_product(const float* restrict a, const float* restrict b, unsigned n);
would calling it with
dot_product(x, x, x_len)
be "undefined", according to the C99 standard?
Edit
x is a pointer, of course, pointing…

MWB
- 11,740
- 6
- 46
- 91
6
votes
3 answers
How to correctly (yet efficiently) implement something like "vector::insert"? (Pointer aliasing)
Consider this hypothetical implementation of vector:
template // ignore the allocator
struct vector
{
typedef T* iterator;
typedef const T* const_iterator;
template
void insert(iterator where, It begin, It end)
…

user541686
- 205,094
- 128
- 528
- 886
5
votes
1 answer
Clang's __restrict is inconsistent?
I was working on highly "vectorizable" code and noted that regarding the C++ __restrict keyword/extension ~, Clang's behavior is different and impractical compared to GCC even in a simple case.
For compiler generated code, the slowdown is about 15x…

Etienne M
- 604
- 3
- 11