Export (0) Print
Expand All

7.5 Pointer Casts

Visual Studio .NET 2003

It is extremely important for the garbage collector to track all pointers into the common language runtime heap during program execution. This leads to the following constraint on __gc pointer casts.

Constraint

  • A __gc pointer shall not be cast to a __nogc pointer.

    Example

    // __gc_pointer10.cpp
    // compile with: /clr
    #using <mscorlib.dll>
    __gc struct G { int i; };
    
    int main() {
       G *pG = new G;
       int *pi;
       pi = &pG->i;         // C2440
       pi = (int*)&pG->i;   // C2440
    }
    

It is possible to convert a __gc pointer to a __nogc pointer via pinning (Section 7.7).

The Managed Extensions maintain the usual meanings of the various styles of C++ casts, limiting them somewhat to preserve the integrity of the common language runtime garbage collector. The following sections describe the impact on the Managed Extensions for each style of cast.

Show:
© 2014 Microsoft