国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
fixed Statement (C#)
C# Language Reference
fixedStatement (C# Reference)

 

Thefixed statement prevents the garbage collector from relocating amovable variable. The fixed statement is only permitted in an unsafecontext. Fixed can also be used to create fixedsize buffers.

Remarks

The fixed statement sets apointer to a managed variable and "pins" that variable during theexecution of statement. Without fixed,pointers to movable managed variables would be of little use sincegarbage collection could relocate the variables unpredictably. The C#compiler only lets you assign a pointer to a managed variable in a fixedstatement.

// assume class Point { public int x, y; }// pt is a managed variable, subject to garbage collection.Point pt = new Point();// Using fixed allows the address of pt members to be// taken, and "pins" pt so it isn't relocated.fixed ( int* p = &pt.x ){*p = 1;}

Youcan initialize a pointer with the address of an array or a string:

        fixed (int* p = arr) ...  // equivalent to p = &arr[0]fixed (char* p = str) ... // equivalent to p = &str[0]

Youcan initialize multiple pointers, as long as they are all of the sametype:

fixed (byte* ps = srcarray, pd = dstarray) {...}

Toinitialize pointers of different type, simply nest fixedstatements:

        fixed (int* p1 = &p.x){fixed (double* p2 = &array[5]){// Do something with p1 and p2.}}

Afterthe code in the statement is executed, any pinned variables areunpinned and subject to garbage collection. Therefore, do not point tothose variables outside the fixed statement.

Note

Pointers initialized in fixed statements cannot be modified.

In unsafe mode, you can allocatememory on the stack, where it is not subject to garbage collection andtherefore does not need to be pinned. For more information, see stackalloc.

Example

// statements_fixed.cs// compile with: /unsafeusing System;class Point{public int x, y;}class FixedTest{// Unsafe method: takes a pointer to an int.unsafe static void SquarePtrParam (int* p){*p *= *p;}unsafe static void Main(){Point pt = new Point();pt.x = 5;pt.y = 6;// Pin pt in place:fixed (int* p = &pt.x){SquarePtrParam (p);}// pt now unpinnedConsole.WriteLine ("{0} {1}", pt.x, pt.y);}}

Output

25 6
C# Language Specification

For more information, see thefollowing sections in the C#Language Specification:

  • 18.3 Fixed and moveable variables

  • 18.6 The fixed statement

Community ContentAdd
fixed statement is not alwaysnecessary
The marshaler will automatically pin blittable referencetypes, such as an array, being passed to native code, for the durationof the call. It cannot do this with raw pointers because it doesn't knowhow much memory they point to. This is also not sufficient if the APIretains the pointer, for example to call back asynchronously. In thatcase you must pin explicitly.
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
How to Write an Opening Statement
C#unsafe與fixed關(guān)鍵字
golang彈出消息框
新詞新譯_D_13
C++中聯(lián)合體union套結(jié)構(gòu)體struct轉(zhuǎn)換為C#結(jié)構(gòu)體
Writing Reentrant and Thread-Safe Code
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服