Strict Aliasing do c

Asked

Viewed 76 times

-4

Guys I would like someone to explain me this such Strict Aliasing , and with cast examples , I saw in a book more did not understand right.

  • Put the example of the book in your statement.

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 guaranteed the two memory areas (dst[0.n] and src[0.n]) do not overlap.

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

All this made the implementation of memcpy() slower. C99 established the Strict aliasing rule, which allows the compiler to assume that the pointers are not pathological, i.e., 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 idea of the programmer is to read the contents of the whole structure.

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

0

If I’m not wrong, Strict Aliasing refers to the passage of variable values or struct 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 going on?

sint, x, and num have the same size(4 bytes), so it’s no problem if 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 spaces were not allocated correctly, it would certainly give memory error.

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

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

Reference

Cell Performace

Blog Qt

Thiemonagel

Blog Regehr

Browser other questions tagged

You are not signed in. Login or sign up in order to post.