<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<head><meta forua="true" http-equiv="Cache-Control" content="max-age=0" /></head>
<card title=".net dll 不能使用 fixed 语句来获取已固定的表达式的地址" id="card1">
<p> 游客</p><p>
标题:.net dll 不能使用 fixed 语句来获取已固定的表达式的地址<br/>
正文:<br/>
<br/>fixed 语句禁止垃圾回收器重定位可移动的变量。 fixed 语句只在不安全的上下文中是允许的。 Fixed 还可用于创建固定大小缓冲区。fixed 语句设置指向托管变量的指针，并在执行该语句期间&amp;ldquo;固定&amp;rdquo;此变量。 如果没有 fixed 语句，则指向可移动托管变量的指针的作用很小，因为垃圾回收可能不可预知地重定位变量。 C# 编译器只允许在 fixed 语句中分配指向托管变量的指针。<br/>unsafe static void TestMethod()<br/>{<br/><br/>    // assume class Point { public int x, y; }<br/>    // pt is a managed variable, subject to garbage collection.<br/>    Point pt = new Point();<br/><br/>    // Using fixed allows the address of pt members to be<br/>    // taken, and &amp;quot;pins&amp;quot; pt so it isn’t relocated.<br/><br/>    fixed (int* p = &amp;amp;pt.x)<br/>    {<br/>        *p = 1;<br/>    }        <br/><br/>}可以用数组或字符串的地址初始化指针：<br/>unsafe void Test2()<br/>{<br/>    Point point = new Point();<br/>    double[] arr = { 0, 1.5, 2.3, 3.4, 4.0, 5.9 };<br/>    string str = &amp;quot;Hello World&amp;quot;;<br/><br/>    fixed (double* p = arr) { /*...*/ }   // equivalent to p = &amp;amp;arr[0]<br/>    fixed (char* p = str) { <br/><a href="http://camnpr.com/wap.asp?mode=WAP&amp;act=View&amp;id=317&amp;Page=1">[&lt;&lt;]</a><a href="http://camnpr.com/wap.asp?mode=WAP&amp;act=View&amp;id=317&amp;Page=1">[[1]]</a><a href="http://camnpr.com/wap.asp?mode=WAP&amp;act=View&amp;id=317&amp;Page=2">[2]</a><a href="http://camnpr.com/wap.asp?mode=WAP&amp;act=View&amp;id=317&amp;Page=3">[3]</a><a href="http://camnpr.com/wap.asp?mode=WAP&amp;act=View&amp;id=317&amp;Page=3">[&gt;&gt;]</a><br/>
<br/>
<a href="wap.asp?act=Com&amp;id=317">查看评论(0)</a><br/>
<a href="wap.asp?act=AddCom&amp;inpId=317">发表评论</a><br/><br/>

<br/>

<br/>
<a href="http://camnpr.com/wap.asp">首页</a>
</p>
</card>
</wml>