Reputation: 113
About the example below, in f1
, no alias occurs because p
(void*
) isn't accessible and p1
is the only pointer accessing memory.
However, there is a pointer aliasing between p1
(float*
) and p2
(int*
) which is outside f1
.
My question is that, is this alias illegal or not, that is, does the strict-aliasing rules apply across function calls ?
If this example is valid, what if the f1
is inlined ?
void f1(void *p)
{
auto* p1 = static_cast<float*>(p);
*p1 = 1.f;
}
int f2()
{
int x = 1;
auto* p2 = &x;
f1(&x);
*p2 = 1;
return *p2;
}
Upvotes: 1
Views: 242
Reputation: 81247
Compilers that are suitable for low-level programming on platforms with a published ABI will provide a means of forcing function calls to be performed in a fashion consistent with that ABI, which would in turn force "strict aliasing violations" that cross the function-call boundary to be handled "in a documented fashion characteristic of the environment". Such support is a "popular extension" which the authors of the Standard regard as outside their jurisdiction. Compilers that are designed and configured to be suitable for low-level programming will support such constructs without regard for whether the Standard requires it, and those which aren't designed and configured to be suitable for such purposes shouldn't be used for them.
Upvotes: 0
Reputation: 66449
It doesn't matter how many times you copy a pointer or pass it to somewhere else or how many times you convert it, the determining factor is always what's actually stored at that location.
In your case, the only thing that matters is whether static_cast
's argument actually is the address of a float
, and it isn't.
Upvotes: 8