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
2 answers

memcpy alias int to char yields UB?

Strict aliasing makes me paranoid. There are times when I set values with an *int pointer and expect the targeted memory to read the same data no matter what the reading pointer type is. Strict aliasing doesn't guarantee this and sometimes even…
0
votes
1 answer

Can a pointer to struct alias an unsigned char array?

#include "stdio.h" /* array to store data receeived from CAN Bus */ unsigned char a[8] = {0xCD, 0xEF, 0x12, 0x34, 0x50, 0x00, 0x00, 0x00}; typedef struct { unsigned int a:12; unsigned int b:12; unsigned int c:12; unsigned int…
Basha
  • 17
  • 2
0
votes
0 answers

Questions about the "Strict Alias Rule" in C and C++

I know this is going to end up being a very lengthy question (with many sub-questions) so please accept my apologies in advance. This last Friday, I was optimizing a particular algorithm to use my processor's native word size to perform bit-wise…
digitale
  • 645
  • 4
  • 13
0
votes
3 answers

Will this strict-aliasing rule violation have the behavior I expect?

I know violating the strict-aliasing rule is Undefined Behavior as per the C standard. Please don't tell me it is UB and there is nothing to talk about. I'd like to know if there are compilers which won't have the expected behavior (defined by me…
atturri
  • 1,133
  • 7
  • 13
0
votes
1 answer

Avoiding strict aliasing violation in hash function

How I can avoid strict aliasing rule violation, trying to modify char* result of sha256 function. Compute hash value: std::string sha = sha256("some text"); const char* sha_result = sha.c_str(); unsigned long* mod_args = reinterpret_cast
Mykola
  • 3,343
  • 6
  • 23
  • 39
0
votes
1 answer

Reason why this reinterpret_cast crashes

Background: I work a lot with binary data, and I often need to work with raw pointers. I also often need the size so that I can check if I read/write off the bounds (reasonable, right?). Now I'm trying to create a syntactic sugar class for pointers…
rr-
  • 14,303
  • 6
  • 45
  • 67
0
votes
2 answers

Can aligned structs inside a union be cast to the union to access aligned fields?

I'm trying to grok what exactly you get from the easement on aligned variables in C99: Exception to strict aliasing rule in C from 6.5.2.3 Structure and union members Does it give you carte blanche on casting to that union, if the original write was…
0
votes
1 answer

Aliasing rule applicable to char strings

I don't understand why this code triggers off aliasing warning: char buf[15]; *(uint16_t*) buf = 0x4040; There is no any thinkable "alignments" within char type. And anyway, how do I do this in graceful way? So far I can't think of anything better…
wick
  • 1,995
  • 2
  • 20
  • 31
0
votes
0 answers

C++: Bypassing strict-aliasing through union, then use __restrict extension

I wonder if it is possible to tailor strict aliasing requirements to specifically designed cases, while still preserving strict aliasing in general or -O2/-O3 optimization respectively. To be more precise, in cases where it is needed, strict…
rsp1984
  • 1,877
  • 21
  • 23
0
votes
1 answer

Casting from member function pointer to another type and back, strict-aliasing issue?

I wrote a class to store either a function pointer or a member function pointer (not both at a time). When I store the member function pointer, I store an object pointer too (the receiver). The problem is: I don't know in advance neither the…
Daniel
  • 2,657
  • 1
  • 17
  • 22
0
votes
2 answers

Undefined-Behavior at its best, is it -boundary break? -bad pointer arithmetic? Or just -ignore of aliasing?

I'm working now for some weeks with c99 focusing undefined behaviour. I wanted to test some strange code while trying to respect the rules. The result was this code: (plz forgive me the variable names, i had eaten a clown) int main(int arg, char**…
dhein
  • 6,431
  • 4
  • 42
  • 74
0
votes
1 answer

how to make tshark executable

After downloading and extracting wireshark 1.7.1, I did a configure with gtk disabled. But then , I am unable to do "make" It throws the following error. cc1: warnings being treated as errors packet-h248_annex_e.c:679: warning: dereferencing…
Dcoder
  • 379
  • 2
  • 7
  • 13
-1
votes
1 answer

Can aliasing info simplify this assembly

I have the following code: void palette(char* in, int* out, int* palette, int n) { for(int i = 0; i < n ; ++i) { int value = palette[in[i]]; out[i] = value; } } Here's the compiler code…
lezebulon
  • 7,607
  • 11
  • 42
  • 73
-1
votes
1 answer

Bug casting from bool* to void* to int*

Often in C++, one has a parameter void* user_data that one can use to pass an arbitrary type. I used this to pass an array of booleans. However, I had a bug where I cast from bool* --> void* --> int* and I got weird results. Here is an…
thedoctar
  • 8,943
  • 3
  • 20
  • 31
-1
votes
1 answer

Is casting incomplete struct pointers undefined behavior?

I was currently reading about some strict aliasing rules, and I was wondering whether casting a pointer to an incomplete struct is undefined behavior. Example 1: #include struct abc; int main(int argc, char *argv[]) { struct abc…
Julius
  • 1,155
  • 9
  • 19
1 2 3
37
38