L12 - managed arrays

L12 - managed arrays - Dynamic Arrays and .NET Managed...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Dynamic Arrays and .NET Managed Arrays Standard C++ arrays are static things: – Once created, they stay the same size – They don’t “know” their own size – There is no automatic bounds checking Visual C++ 2005 offers an extension: managed arrays – See Chapter 5 of the C++/CLI text – these are actually objects of the class System::Array – This means that they can use all the methods of this class – Every managed array knows its own length You can obtain it via arrayname -> Length Remember the difference between arrow operator and dot operator: Dot accesses a class member via the variable name or via a reference to the object (i.e. accesses methods of an object) Arrow accesses a class member via a pointer to the object.(i.e. methods of a class) An array name (for a managed array) is a tracking handle – Like standard C++ arrays, managed arrays are “static” in the sense that they stay the same size once they are created. If you need to have an array that can grow and shrink in size, then you’ll have to use the class ArrayList or handle it explicitly yourself
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Managed and Unmanaged Arrays Standard C++ arrays are unmanaged arrays If you declare an array using any of the aliases for standard C++ types, the array is by default an unmanaged one The standard aliases are: int, double, bool, char, etc. Unmanaged arrays are static and behave just as we have discussed so far. Since managed arrays are objects and all managed objects have to be created with gcnew, managed arrays have to be created with gcnew in order to allocate space for them: array<int>^ xnums; // this declares a managed array To allocate space for the array and set the tracking handle to it: xnums = gcnew array<int>(25); When managed arrays are allocated, the elements are initialized to 0 for numeric types, to false for bool, and to 0 for pointers gcnew is a keyword that is used in allocating space for a managed type. Managed types get garbage collected automatically.
Background image of page 2
Managed and Unmanaged Arrays (cont.) In C++, when declaring an array, you must specify the dimension of the array. In Visual C++ 2005, when declaring a managed array, you are not allowed to specify the dimension of the array. – Unlike regular variables (those variables declared with primitive
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 17

L12 - managed arrays - Dynamic Arrays and .NET Managed...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online