Strict Aliasing of the c [closed]

-4

Galera would like someone to explain this Strict Aliasing, and with cast examples, I saw in a book I did not understand.

    
asked by anonymous 21.04.2016 / 02:50

2 answers

2

Strict aliasing is when the compiler assumes that two different pointers do not point to the same memory area. It is a presumption created in the C99 standard that aims to improve performance.

For example, the memcpy (dst, src, n) function copies "n" bytes from the "src" position to the "dst" position. This function could work much more efficiently if the two memory areas (dst [0..n] and src [0..n]) do not overlap.

Because if they overlap, the copy would have to start from one side or the other, depending on the situation. If dst > src, the copy can start from position zero, on the other hand if dst < src, the copy has to start from the end and walk backwards. There are still cases where the two buffers are completely overlapped (dst = src).

All this made the implementation of memcpy () slower. C99 has established the strict aliasing rule, which allows the compiler to assume that pointers are not pathological, that is, they do not overlap. With this, memcpy () can be as fast as possible (if the CPU copies faster backwards, this is how it will be done).

Another typical situation is when two pointers of different types point to the same area of memory. For example, a pointer to any structure, and another pointer int * to the same structure. Maybe the programmer's idea is to read the contents of the entire structure in its entirety.

But by the strict aliasing rule, the compiler does not have to assume that both pointers cover the same thing, which can cause all kinds of strange behavior. To correctly establish that two pointers of different types point to the same memory, one should use a "union".

    
21.04.2016 / 07:45
0

If I am not wrong, Strict Aliasing refers to passing variable values or struct to each other of different types. Ex:

#include <stdio.h>
#include <malloc.h>
#include <string.h>

typedef struct{
        short x;
        short y;
}strc_int;

void main(){
    strc_int *sint = malloc(sizeof(strc_int));
    int *x = malloc(sizeof(int));;
    char num[] = {4,1,140,10};

    memcpy(sint,num,4);

    x = (int*) sint;

    printf("%d - %d\n",sint->x, sint->y);
    printf("%d\n", x);
}
  

What's happening?

sint , x , and num have the same size (4 bytes), so no problem I pass the values from one to the other.

This is only possible because values have structures.

'char'     ocupa 1 byte  []
'short'    ocupa 2 bytes [][]
'int'      ocupa 4 bytes [][][][]
'strc_int' ocupa 4 bytes [][][][]

If not the spaces were not allocated correctly, it would certainly give memory error.

Many programmers use this practice, some without knowing that it even has a specific name (As was my case).

But I think it's not good practice for inexperienced programmers, because memory errors are the worst to solve.

Reference

Cell Performace

Blog Qt

Thiemonagel

Blog Regehr

    
21.04.2016 / 03:34