This documentation is archived and is not being maintained.

4.5 __gc Arrays

Visual Studio .NET 2003

A __gc array is a dynamic array that is allocated on the common language runtime heap. The number of elements of the array is not part of the type. A single array variable may refer to arrays of different sizes.

Example

// __gc_arrays.cpp
// compile with: /clr
#using <mscorlib.dll>
using namespace System;

int main() {
   Int32 ia[] = __gc new Int32[100];
   ia = new Int32[200];
}

The indices of a __gc array are zero-based, as in standard C++. A __gc array is subscripted using ordinary C++ array brackets. Unlike standard C++, subscripting is not a synonym for pointer arithmetic, and is not commutative.

Characteristics

  • A __gc array shall be allocated using the managed operator __gc new.
  • Using new to allocate a managed array defaults to __gc new.

Constraints

  • The type of an element of a __gc array shall only be a __value class, or a __gc pointer to a __gc class.
  • __nogc new shall not be used to allocate an array of managed type.
  • A __gc array variable definition shall not specify a size. The size of the array is given in the call to the managed operator __gc new.

A __gc array is itself a __gc object. It is actually a pointer into the common language runtime heap. The indirection of the pointer has been factored into the subscripting operator. As a __gc object, it has the same restrictions as a __gc class (Section 4). Most notably, the element type cannot be an unmanaged C++ class that is not a POD type.

All __gc arrays inherit from System::Array. Any method or property of System::Array can be applied directly to the array variable.

Example

// __gc_arrays2.cpp
// compile with: /clr
#using <mscorlib.dll>
using namespace System;

int main() {
   Int32 ia[] = __gc new Int32[100];
   Console::WriteLine(ia->Length);
}

Output

100
Show: