• <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語(yǔ)法中,值類(lèi)型指針?lè)譃閮煞N類(lèi)型:V*(限于非 CLR 堆位置)和內(nèi)部指針 interior_ptr<V>(允許但不要求托管堆內(nèi)的地址)。


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

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

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

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

            在新的語(yǔ)言設(shè)計(jì)中,釘住指針是使用類(lèi)似于內(nèi)部指針的語(yǔ)法來(lá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 天下 閱讀(1096) 評(píng)論(0)  編輯 收藏 引用 所屬分類(lèi): C/C++C#

            <2015年12月>
            293012345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789

            導(dǎo)航

            統(tǒng)計(jì)

            常用鏈接

            留言簿(4)

            隨筆分類(lèi)(378)

            隨筆檔案(329)

            鏈接

            最新隨筆

            搜索

            最新評(píng)論

            成人国内精品久久久久影院| 久久久久亚洲AV无码专区网站 | 久久影院午夜理论片无码| 久久免费国产精品| 亚洲国产美女精品久久久久∴| 国内精品久久久人妻中文字幕| 久久国产三级无码一区二区| 久久精品人人做人人爽电影| 久久亚洲国产精品一区二区| 久久妇女高潮几次MBA| 91超碰碰碰碰久久久久久综合| 亚洲精品乱码久久久久久蜜桃不卡| 91精品日韩人妻无码久久不卡| 亚洲精品无码成人片久久| 精品久久人人妻人人做精品 | 精品久久香蕉国产线看观看亚洲| 久久久久久久久久久免费精品| 日韩精品久久无码人妻中文字幕| 久久精品国产精品亜洲毛片| 精品久久久久久国产潘金莲| 欧美日韩精品久久免费| 99久久国产免费福利| 久久精品国产网红主播| 色综合久久夜色精品国产| 91久久福利国产成人精品| 国产精品一区二区久久不卡| 亚洲精品无码久久久久久| 伊人色综合久久天天人守人婷 | 2020久久精品国产免费| 亚洲精品无码成人片久久| 国产香蕉久久精品综合网| 亚洲精品成人久久久| 久久99精品国产99久久6| 久久亚洲国产中v天仙www | 久久精品国产WWW456C0M| 欧美精品一区二区精品久久 | 99久久国产热无码精品免费| 久久天天躁夜夜躁狠狠躁2022 | 亚洲精品高清一二区久久| 精品久久综合1区2区3区激情| 久久亚洲国产精品一区二区|