| Start |
|---|
| Aliasing and effective type |
| restrict qualification If we have two pointer arguments of the same type, the compiler can't make any assumption and will always have to assume that the change to all goes well and something like is 4 equal to 4? is printed. If we pass the same pointer, the program will still do the right thing and print is 4 equal to 22? This can turn out to be inefficient, if we know by some outside information that Then the compiler may always suppose that |
| Changing bytes Once an object has an effective type, you should not attempt to modify it through a pointer of another type, unless that other type is a character type, This is a valid program that prints a now has value 707406378 This works because:
|
| Character types cannot be accessed through non-character types. If an object is defined with static, thread, or automatic storage duration, and it has a character type, either: This is undefined because it violates the "effective type" rule, no data object that has an effective type may be accessed through another type that is not a character type. Since the other type here is Even if alignment and pointer sizes would be known to fit, this would not exempt from this rule, behavior would still be undefined. This means in particular that there is no way in standard C to reserve
a buffer object of character type that can be used through pointers
with different types, as you would use a buffer that was received by
A correct way to achieve the same goal as in the above example would
be to use a Here, the |
| Violating the strict aliasing rules In the following code let us assume for simplicity that
That is, the second load operation of If we call this function "normally" all goes well and something like 4 should equal 4 is printed. But if we cheat and pass the same pointer, after converting it, we violate the strict aliasing rule. Then the behavior becomes undefined. The output could be as above, if the compiler had optimized the second access, or something completely different, and so your program ends up in a completely unreliable state. RemarksViolations of aliasing rules and of violating the effective type of an object are two different things and should not be confounded. Aliasing can be a problem for optimization, because changing the object through one pointer, C's strict aliasing rules refers to cases in the compiler may assume which objects do (or do not) alias each other. There are two rules of thumb that you always should have in mind for data pointers. Unless said otherwise, two pointers with the same base type may alias. Two pointers with different base type cannot alias, unless at least one of the two types is a character type. Here base type means that we put aside type qualifications such as Violating the second rule can have catastrophic results. Here violating the strict aliasing rule means that you present two pointers If you do so the behavior of your program becomes undefined. Therefore, C puts quite severe restrictions on pointer conversions in order to help you to avoid such situation to occur accidentally. Unless the source or target type is `void`, all pointer conversions between pointers with different base type must be **explicit**. Or in other words, they need a cast, unless you do a conversion that just adds a qualifier such as Avoiding pointer conversions in general and casts in particular protects you from aliasing problems. Unless you really need them, and these cases are very special, you should avoid them as you can. |
Read Article
https://develop.horje.com/learn/1434/reference