Previous: Invoking Functions Through Global Pointers Up: Global Pointers Next: Pitfalls
Because of this danger, CC++ will not implicitly cast a global pointer to a local pointer. If the global pointer really references memory on the current processor object, then an explicit cast can be used.
{
int *global gpi;
int* pi= (int *)gpi; // Think Carefully Before Using!
}
An explicit cast must only be used when it is certain that the global pointer references memory in the local processor object. A run-time error results if this is not the case.
Local pointers are implicitly, and can also be explicitly, cast into global pointers. Here are the four possibilities:
{
int* pi;
int *global gpi = pi; // Implicit local-to-global cast OK
int *global gpi2 = (int *global)pi; // Explicit local-to-global cast OK
pi = gpi; // Implicit global-to-local cast COMPILE-TIME ERROR
pi = (int *)gpi; // Explicit global-to-local cast POSSIBLE RUN-TIME ERROR
}
In this example, the explicit global-to-local cast would not be an error, since we initialized gpi using the local pointer pi!