Questions tagged [strict-aliasing]

Strict aliasing is an assumption, made by the C or C++ compiler, that de-referencing pointers to objects of different types will never refer to the same memory location (i.e. they will not alias each other).

Strict aliasing is an assumption, made by the C or C++ compiler, that de-referencing pointers to objects of different types will never refer to the same memory location (i.e. they will not alias each other).

Strict aliasing or "the strict aliasing rule" are informal terms. It refers not to one, but to a set of rules regarding type compatibility when accessing a value through pointers.

A "strict aliasing violation" is when a value is accessed through a pointer which is not compatible with the data type. Example:

short array[2];
int* ptr = (int*)array;       // strict aliasing violation
if(*ptr == something)         // therefore, this is undefined behavior

Strict aliasing is explained in detail here: What is the strict aliasing rule?

The reason why the strict aliasing exists is performance, as discussed here. A compiler which assumes that the strict aliasing rule is followed can make various optimizations to increase performance.

Traditionally, not many compilers have done such optimizations, and therefore the undefined behavior caused by strict aliasing violations is a common dormant bug in many programs - a bug that will not surface until the program is ported to a compiler which uses such optimizations.

The GCC compiler in particular is known to frequently do such optimizations. They are automatically enabled with compiler options -O2, -O3 or -Os, or explicitly with -fstrict-aliasing. Strict aliasing optimizations can be disabled with -fno-strict-aliasing. GCC reference.


The aliasing rules are described in the following sections in the standards:

C11 6.5 §7

An object shall have its stored value accessed only by an lvalue expression that has one of the following types:
— a type compatible with the effective type of the object,
— a qualified version of a type compatible with the effective type of the object,
— a type that is the signed or unsigned type corresponding to the effective type of the object,
— a type that is the signed or unsigned type corresponding to a qualified version of the effective type of the object,
— an aggregate or union type that includes one of the aforementioned types among its members (including, recursively, a member of a subaggregate or contained union), or
— a character type.

C++11 3.10 §10.

If a program attempts to access the stored value of an object through a glvalue of other than one of the following types the behavior is undefined:
— the dynamic type of the object,
— a cv-qualified version of the dynamic type of the object,
— a type similar (as defined in 4.4) to the dynamic type of the object,
— a type that is the signed or unsigned type corresponding to the dynamic type of the object,
— a type that is the signed or unsigned type corresponding to a cv-qualified version of the dynamic type of the object,
— an aggregate or union type that includes one of the aforementioned types among its elements or nonstatic data members (including, recursively, an element or non-static data member of a subaggregate or contained union),
— a type that is a (possibly cv-qualified) base class type of the dynamic type of the object,
— a char or unsigned char type.

563 questions
0
votes
1 answer

c++ array of templated abstract-base-class without breaking strict-aliasing rule

What is the best way to create an array of objects derived from an abstract template base class without violating the strict-aliasing rule. Each derived object will define the template arguments of the base class differently but only through the…
0
votes
1 answer

Breaking the strict-aliasing rule in the simplest assignment

After reading the Understanding Strict Aliasing article https://cellperformance.beyond3d.com/articles/2006/06/understanding-strict-aliasing.html I see, how breaking the strict-aliasing rules can cause unexpected results in optimized build. For…
Alex F
  • 42,307
  • 41
  • 144
  • 212
0
votes
1 answer

C++ type aliasing, where value is replaced

Is the following code legal in C++? int get_i(int idx) { ... } float transform(int i) { ... } void use(float f) { ... } static_assert(sizeof(int) == sizeof(float)); void* buffer = std::malloc(n * sizeof(int)); int* i_buffer =…
tmlen
  • 8,533
  • 5
  • 31
  • 84
0
votes
3 answers

How to indicate absence of aliasing for a struct member?

If you write statements like: a[i] = b[i] + c[i]; ...you might want to indicate to the compiler that a[i], b[i] and c[i] point to different places in memory, thus enabling various optimizations (e.g. vectorization). This can be done by adding a…
Petr Mánek
  • 1,046
  • 1
  • 9
  • 24
0
votes
1 answer

Violating strict aliasing does not always produce a compiler warning

Strict-aliasing has kinda thrown me into a loop. Here's the code. I have a class #include #include class Alias { public: struct rtentry rt; struct sockaddr_in *address; void try_aliasing() { address…
mitgorakh
  • 11
  • 2
0
votes
1 answer

Does Aliasing/Alignment issues occur for an Array within a Structure?

If we have an array within a struct: struct Names { uint8 fileId; uint8 name[50]; }; and then we try to assign a uint16 from the array to a uint16 variable like: uint16 someName = *((uint16 *)&NamesObj.name[21]); Will this violate aliasing…
AlphaGoku
  • 968
  • 1
  • 9
  • 24
0
votes
0 answers

strict aliasing within struct

I have a class that I use to reinterpret data, but the underlying data is of the same type and yet doing this seems to violate strict aliasing. I would like to be able to reinterpret_cast to choose how to interpret the data. Here's an…
nachum
  • 567
  • 9
  • 17
0
votes
1 answer

Array-like container implementation vs strict aliasing

I'm trying to implement an array-like container with some special requirements and a subset of std::vector interface. Here is a code excerpt: template class MyArray { public: explicit MyArray(const uint32_t size) : storage(new…
Sergey
  • 7,985
  • 4
  • 48
  • 80
0
votes
1 answer

Useless case for restrict

So my understanding is that the C99 standard mandates that pointers to different types should not be aliased (i.e. pointed to the same memory). The restrict keyword assures the compiler that two certain variables (of the same type?) are not located…
Daniel Lovasko
  • 471
  • 2
  • 10
0
votes
3 answers

Strict-Aliasing warnings and tcpdump example code

Simplifying the concept, the strict-aliasing rule states that an object should be accessed by a pointer of a compatible type or a pointer to char. That way, the compiler can make some assumptions about the code and make some optimizations. Even…
murphsghost
  • 161
  • 1
  • 7
0
votes
1 answer

Strict aliasing and union of char arrays

I sort of expect what the answer will be already, but am curious what the standard says about this. The setup: I want to control the exact offset of fields in a structure, and specify them directly in the field type. Here is the magic: #include…
Alexander Kondratskiy
  • 4,156
  • 2
  • 30
  • 51
0
votes
1 answer

Does char[] + memcpy() violate strict aliasing?

I'm using a char array in a struct to hold some generic data, like this (the input type may be a struct of unknown size so I can't just use a union; this code is heavily simplified): typedef struct { char buf[256]; } data; void…
Andrew Sun
  • 4,101
  • 6
  • 36
  • 53
0
votes
1 answer

C++: Casting from char pointer to struct pointer without committing undefined behavior

struct S { char A; char B; char C; char D; }; unsigned char x[4] = { 0xDE, 0xAD, 0xBE, 0xEF }; auto y = (S*) x; cout << y->A; // undefined behaviour The fourth line here violates strict aliasing, which means that the compiler could decide to play…
Carl Patenaude Poulin
  • 6,238
  • 5
  • 24
  • 46
0
votes
1 answer

warning! dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]

I have problem with aliasing. I have never faced with it before. I am using Eclipse CDT. I read different solutions but I couldn't find the suitable one for me. I have the warning dereferencing type-punned pointer will break strict-aliasing rules…
0
votes
1 answer

Is aliasing of pointers between aggregate C structs and their members standards-compliant?

Is the following a standards-compliant C program (and by which standard(s))? If it is non-compliant (by breaking the strict aliasing rule or otherwise) is it "safe" with respect to GCC or any other commonly-used C compiler? int main() { typedef…
Patrick Sanan
  • 2,375
  • 1
  • 23
  • 25