C ++ variable alias - what exactly is it, and why is it better to disconnect if disconnect?

I read the essay. I survived the version version .

The section "Aliasing errors" says:

You can get stricter code if you tell the compiler that it cannot accept anti-aliasing ....

I also read Aliasing (calculations) .

What is a variable alias? I understand this means that using a pointer to a variable is an alias, but how / why it has a big effect, or, in other words, why telling the compiler that it cannot use aliases will lead me to a "harder code"

+6
c ++
source share
2 answers

Preventing aliasing means that if you have a char* b pointer, you can assume that b is the only pointer in the program that points to that particular memory location, which means that the only time the memory location will change is when a programmer uses b to change it. Thus, the generated assembly does not need to reload the memory pointed to by b into the register, if the compiler does not know anything, used b to change it. If overlay is allowed, perhaps there is another pointer char* c = b; that was used elsewhere to interfere with this memory

+5
source share

Smoothing is when you have two different references to the same base memory. Consider this example:

 int doit(int *n1, int *n2) { int x = 0; if (*n1 == 1) { *n2 = 0; x += *n1 // line of interest } return x; } int main() { int x = 1; doit(&x, &x); // aliasing happening } 

If the compiler should allow anti-aliasing, it should take into account the possibility of n1 == n2 . Therefore, when he needs to use the value *n1 in the line "interest", he needs to allow the possibility of changing it on the line *n2 = 0 .

If the compiler cannot use aliases, it can accept in the "line of interest" that *n1 == 1 (because otherwise we would not be inside the if ). Then the optimizer can use this information to optimize the code (in this case, change the "line of interest" from the following pointer and make a general-purpose addition to use a simple increment).

+12
source share

All Articles