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
-1
votes
1 answer

Strict aliasing exceptions explanations

First two exceptions of strict aliasing rule on cppreference.com says: AliasedType is (possibly cv-qualified) DynamicType AliasedType and DynamicType are both (possibly multi-level, possibly cv-qualified at each level) pointers to the same type T…
αλεχολυτ
  • 4,792
  • 1
  • 35
  • 71
-1
votes
3 answers

typecast a char * to int *

I have a given string char *names = "ABC"; Now as part of understanding pointer and it casting I want to converting the string to it ascii code but using pointer. Here what I have done thus far. char *name = "ABC"; int *array; array = (int *)…
Ratatouille
  • 1,372
  • 5
  • 23
  • 50
-1
votes
2 answers

Aliasing in ANSI C : Is a = (double *) (&a) allowed

I would like to know if there is anything that prevents doing this in ANSI C (or anything prior to C99 which has the strict aliasing rule). const int n = 1000; double *a = (double *) malloc(n * sizeof(double)); // Weird aliasing a = (double *)…
InsideLoop
  • 6,063
  • 2
  • 28
  • 55
-1
votes
2 answers

Casting small fields in a structure to a larger variable

I have a situation in a legacy code with a large field of a structure being split into two sub-fields. For example, a uint32 is split into two uint16's: typedef struct { uint16 myVar_H; uint16 myVar_L; } MyStruct; Until now these fields…
Eli Iser
  • 2,788
  • 1
  • 19
  • 29
-2
votes
3 answers

Is having a buffer of unsigned char and treating it as a pointer to T* a violation of strict aliasing?

Some sources say the following is a violation of strict aliasing: #include typedef struct Foo { int a; double* b; } Foo; int main() { _Alignas(Foo) unsigned char buffer[2048]; Foo* a = (Foo*)&buffer[0]; a->a = 44; …
Raildex
  • 3,406
  • 1
  • 18
  • 42
-2
votes
3 answers

C dummy struct, strict aliasing and static initialization

My first question wasn't well formulated so here goes again, this time, more well asked and explained. I want to hide the variables of a struct while being able to initialize the struct statically on the stack. Most solutions out there use the…
João Pires
  • 927
  • 1
  • 5
  • 16
-2
votes
1 answer

Need help to resolve warning: dereferencing type-punned pointer will break strict-aliasing rules

I am working on a set of C code to optimize it. I came across a warning while fixing a broken code. The environment is Linux, C99, compiling with -Wall -O2 flags. Initially a struct text is defined like this: struct text { char…
Workarea
  • 7
  • 4
-2
votes
1 answer

Stricit aliasing violation: Why gcc and clang generate different output?

When the typecasting violates the strict aliasing rule in C and C++, a compiler may optimize in such a way that wrong constant value can be propagated and unaligned access could be allowed, which results in performance degradation or bus errors. I…
ruach
  • 1,369
  • 11
  • 21
1 2 3
37
38