• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            天下

            記錄修行的印記

            pin_ptr 和 interior_ptr

            CLR語法中,值類型指針分為兩種類型:V*(限于非 CLR 堆位置)和內(nèi)部指針 interior_ptr<V>(允許但不要求托管堆內(nèi)的地址)。


            釘住指針
            通常,在壓縮階段,垃圾回收器可選擇將駐留在 CLR 堆上的對象移動到堆內(nèi)的不同位置。這種移動對于跟蹤句柄、跟蹤引用和內(nèi)部指針而言不是問題,它們都透明地更新這些實體。但是,如果用戶在運行時環(huán)境外傳遞了 CLR 堆上對象的地址,這種移動就會導(dǎo)致問題。在這種情況下,對象的不穩(wěn)定運動可能導(dǎo)致運行時失敗。要避免此類對象的移動,必須在本地將它們釘在自己的位置上以供外部使用。

            在托管擴展中,“釘住指針”是通過使用 __pin 關(guān)鍵字限定指針聲明來聲明的。下面是一個來自托管擴展規(guī)范的經(jīng)過略微修改的示例:

              復(fù)制代碼 
            __gc struct H { int j; };

            int main() 
            {
               H * h = new H;
               int __pin * k = & h -> j;
              
               // …
            };
             

            在新的語言設(shè)計中,釘住指針是使用類似于內(nèi)部指針的語法來聲明的。

              復(fù)制代碼 
            ref struct H
            {
            public:
               int j;
            };

            int main()
            {
               H^ h = gcnew H;
               pin_ptr<int> k = &h->j;

               // …
            }



            interior_ptr
            Declares a pointer to inside a reference type, but not to the object itself. An interior pointer can point to a reference handle, value type, boxed type handle, member of a managed type, or to an element of a managed array. 
            cli::interior_ptr<cv_qualifier type> var = &initializer;

            Remarks
            A native pointer is not able to track an item as its location changes on the managed heap, which results from the garbage collector moving instances of an object. In order for a pointer to correctly refer to the instance, the runtime needs to update the pointer to the newly positioned object.

            An interior_ptr represents a superset of the functionality of a native pointer. Therefore, anything that can be assigned to a native pointer can also be assigned to an interior_ptr. An interior pointer is permitted to perform the same set of operations as native pointers, including comparison and pointer arithmetic.

            An interior pointer can only be declared on the stack. An interior pointer cannot be declared as a member of a class.

            Since interior pointers exist only on the stack, taking the address of an interior pointer yields an unmanaged pointer.

            interior_ptr has an implicit conversion to bool, which allows for its use in conditional statements.

            For information on how to declare an interior pointer that points into an object that cannot be moved on the garbage-collected heap, see pin_ptr.

            interior_ptr is in the cli namespace. See cli Namespace for more information.


            //Example
            // interior_ptr.cpp
            // compile with: /clr
            using namespace System;

            ref class MyClass {
            public:
               int data;
            };

            int main() {
               MyClass ^ h_MyClass = gcnew MyClass;
               h_MyClass->data = 1;
               Console::WriteLine(h_MyClass->data);

               interior_ptr<int> p = &(h_MyClass->data);
               *p = 2;
               Console::WriteLine(h_MyClass->data);

               // alternatively
               interior_ptr<MyClass ^> p2 = &h_MyClass;
               (*p2)->data = 3;
               Console::WriteLine((*p2)->data);
            }

             


             

            posted on 2015-12-21 17:11 天下 閱讀(1116) 評論(0)  編輯 收藏 引用 所屬分類: C/C++C#

            <2010年11月>
            31123456
            78910111213
            14151617181920
            21222324252627
            2829301234
            567891011

            導(dǎo)航

            統(tǒng)計

            常用鏈接

            留言簿(4)

            隨筆分類(378)

            隨筆檔案(329)

            鏈接

            最新隨筆

            搜索

            最新評論

            2020国产成人久久精品| 久久精品无码一区二区三区| 久久综合精品国产一区二区三区 | 欧美一级久久久久久久大| 久久只有这精品99| 91视频国产91久久久| 久久久久国产精品嫩草影院| 亚洲AV无码久久| 国内精品久久久久久中文字幕| 久久婷婷国产剧情内射白浆| 久久99国产精品99久久| 国产69精品久久久久APP下载| 国内精品人妻无码久久久影院| 久久久久一本毛久久久| 国产精品久久久久久一区二区三区 | 2021国产精品午夜久久| 草草久久久无码国产专区| 久久精品无码专区免费青青| 久久精品综合网| 国产农村妇女毛片精品久久| 久久国产热精品波多野结衣AV| 亚洲精品高清一二区久久| 久久91精品综合国产首页| 99久久中文字幕| 精品久久8x国产免费观看| 99精品国产综合久久久久五月天 | 久久综合五月丁香久久激情| 亚洲国产精久久久久久久| 性欧美大战久久久久久久久| 亚洲一区精品伊人久久伊人| 久久精品18| 精品欧美一区二区三区久久久| 久久se精品一区二区| 伊人久久综在合线亚洲2019| 久久国产精品99精品国产987| 99久久人妻无码精品系列| 久久国产精品无码一区二区三区| 伊人久久综合成人网| 亚洲精品tv久久久久久久久| 久久人人爽人人爽人人AV | 亚洲欧美日韩中文久久|