C++/cli (v): Tracking handle, trace reference, and internal pointer __c++

Source: Internet
Author: User
Tags arithmetic garbage collection

Unlike the local C + + maintenance heap, the dynamically allocated memory in C++/CLI is maintained by the CLR. When a heap is not needed, the CLR automatically deletes it for recycling, while the CLR can automatically compress the memory heap to avoid unnecessary memory fragmentation. This mechanism avoids memory leaks and memory fragmentation , which is referred to as garbage collection, while this kind of heap managed by the CLR is called the CLR heap. It is created by the operator gcnew.

Because the garbage collection mechanism changes the address of objects in the heap, you cannot use normal C + + pointers in the CLR heap, because if the object address that the pointer points to changes, the pointer is no longer valid. To secure access to heap objects, the CLR provides trace handles (similar to C + + pointers) and trace references (similar to C + +) references. one, tracking handle

A trace handle is similar to a local C + + pointer, but can be automatically updated by the CLR garbage collector to reflect the new address of the object being tracked. Arithmetic operations that address the trace handle are not allowed, and coercion type conversions are not possible.

Objects created on the CLR heap must be referenced by a trace handle, which includes: (1) Display the object created on the heap with the gcnew operator, (2) All reference data types (the numeric type is assigned on the stack by default). Note: All objects that are allocated on the heap cannot be created in the global scope.

about how to use tracing handles see "Getting Started with C++/cli Learning (iii): Arrays" two, tracking references

A trace reference is similar to a local C + + reference that represents an alias for an object. You can create a trace reference to a value object on the stack, a trace handle on the CLR heap. The trace reference itself is always created on the stack. If garbage collection moves the referenced object, the trace reference is automatically updated.

Trace references are defined in%, and the following example creates a trace reference to a value object on the stack:

int value = ten;
int% Trackvalue = value;

Stackvalue is a reference to a value variable, you can use Stackvalue to access value:

Trackvalue *= 5;
Console::WriteLine (value); Result is 50
three, internal pointers

C++/CLI also provides an internal pointer defined with the keyword interior_ptr, which allows arithmetic operations for addresses. When necessary, the addresses stored within the pointer are automatically updated by the CLR garbage collection. Note that the internal pointer is always a local automatic variable for the function.

The following code defines an internal pointer that contains the address of the first element in an array:

array<double>^ data = {1.5, 3.5, 6.7, 4.2, 2.1};
Interior_ptr<double> Pstart =%data[0];

You must specify the type of object that the internal pointer points to interio_ptr. You should also initialize the pointer, and if you do not provide an initial value, the system initializes it by default to nullptr.

The internal pointer should note when specifying a type that can contain the address of a value type object on the stack, an address that points to an object handle on the CLR heap, or a local class object or a local pointer, but not the address of an entire object on the CLR heap . That is, you can use an internal pointer to store the address of a numeric class object (such as a CLR array element) that is part of an object on the CLR heap, or you can store the address of a System::String object's tracking handle, but you cannot store the address of the string object itself.

Interior_ptr<string^> pstr1; OK--pointer to a handle
interior_ptr<string>  pstr2;//ERROR--pointer to a String object

As with local C + + pointers, an internal pointer can perform arithmetic calculations. You can change the address that it contains by incrementing or descending to refer to the data item that follows or precedes it, or you can add or subtract an integer on the internal pointer, and you can compare the internal pointer. The following example illustrates the use of internal pointers:------------------------------------------------------------ - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Ex4_19.cpp:main project file.
#include "stdafx.h"
using namespace System;

int main (array<system::string ^> ^args)
{
 array<double>^ data = {1.5, 3.5, 6.7, 4.2, 2.1};
 interior_ptr<double> Pstart = &data[0];
 interior_ptr<double> pend = &data[data->Length-1];
 Double sum = 0;

 while (pstart<=pend)
  sum + = *pstart++;

 Console::WriteLine (L "Total of data array elements = {0}\n", sum);

 array<string^>^ strings = {L "land ahoy!",
  L "Splice the mainbrace!",
  L "Shiver me timbers!",
  L " Never throw into the wind! "
 };

 for (interior_ptr<string^> pstrings = &strings[0]; Pstrings-&strings[0] < strings->length; + + pstrings)
  Console::WriteLine (*pstrings);

    return 0;
}
-----------<<==---------------------------------------------------------- - - - - - - - - - - - - - - - - - - - - -

Output is

 total of data array elements = ahoy!
Splice the mainbrace!
Shiver me timbers! Never throw into the wind! 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.