⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 25.6.htm

📁 This ECMA Standard specifies the form and establishes the interpretation of programs written in the
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Hyperlinked ECMA C# Language Specification</title><meta name="author" content="Jon Jagger" /><link rel="stylesheet" href="ecma334.css"></link></head><body><div align="right"><em><a href="http://www.jaggersoft.com">Jon Jagger</a></em></div><div align="right"><a href="mailto:jon@jaggersoft.com">jon@jaggersoft.com</a></div><form method="get" action="http://search.atomz.com/search/"><input size="30" name="sp-q"></input><input type="submit" value="Search C# Spec"></input><input type="hidden" name="sp-a" value="sp10024177"></input><input type="hidden" name="sp-f" value="ISO-8859-1"></input></form><a href="toc.htm">Table of Contents</a> <a href="1.htm">1</a> <a href="2.htm">2</a> <a href="3.htm">3</a> <a href="4.htm">4</a> <a href="5.htm">5</a> <a href="6.htm">6</a> <a href="7.htm">7</a> <a href="8.htm">8</a> <a href="9.htm">9</a> <a href="10.htm">10</a> <a href="11.htm">11</a> <a href="12.htm">12</a> <a href="13.htm">13</a> <a href="14.htm">14</a> <a href="15.htm">15</a> <a href="16.htm">16</a> <a href="17.htm">17</a> <a href="18.htm">18</a> <a href="19.htm">19</a> <a href="20.htm">20</a> <a href="21.htm">21</a> <a href="22.htm">22</a> <a href="23.htm">23</a> <a href="24.htm">24</a> <a href="25.htm">25</a> <a href="notes.htm">Notes</a> <a href="HyperlinkedCSharpECMA.zip">Download</a><span class="ruler"></span><span class="heading">ECMA-334 C# Language Specification</span><span class="navigate"><a href="25.5.8.htm"><img src="previous.gif" alt="previous" border="0" /></a><a href="25.7.htm"><img src="next.gif" alt="next" border="0" /></a></span><span class="clause-depth"><a href="24.htm"><img src="previous.gif" alt="previous at this level" border="0" /></a><a href="25.htm"><img src="next.gif" alt="next at this level" border="0" /></a> <span class="clause-number-link"><a href="25.htm">25</a></span><span class="clause-title-previous"> Unsafe code</span></span><span class="clause-depth"><a href="25.5.htm"><img src="previous.gif" alt="previous at this level" border="0" /></a><a href="25.7.htm"><img src="next.gif" alt="next at this level" border="0" /></a> <span class="clause-number">25.6</span><span class="clause-title"> The fixed statement</span></span><span class="locator">
     Paragraph 1</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P1S1"></a>In an unsafe context, the <span class="non-terminal"><a href="15.htm#embedded-statement">embedded-statement</a></span> (<a href="15.htm">&#167;15</a>) production permits an additional construct, the fixed statement, which is used to "fix" a moveable variable such that its address remains constant for the duration of the statement.</span> <span class="grammar-production"><span class="name"><a name="embedded-statement"></a>embedded-statement</span> : <span class="rhs"><span class="terminal">...</span> </span><span class="rhs"><span class="non-terminal"><a href="25.6.htm#fixed-statement">fixed-statement</a></span> </span></span><span class="grammar-production"><span class="name"><a name="fixed-statement"></a>fixed-statement</span> : <span class="rhs"><span class="keyword">fixed</span> <span class="terminal">(</span> <span class="non-terminal"><a href="25.2.htm#pointer-type">pointer-type</a></span> <span class="non-terminal"><a href="25.6.htm#fixed-pointer-declarators">fixed-pointer-declarators</a></span> <span class="terminal">)</span> <span class="non-terminal"><a href="15.htm#embedded-statement">embedded-statement</a></span> </span></span><span class="grammar-production"><span class="name"><a name="fixed-pointer-declarators"></a>fixed-pointer-declarators</span> : <span class="rhs"><span class="non-terminal"><a href="25.6.htm#fixed-pointer-declarator">fixed-pointer-declarator</a></span> </span><span class="rhs"><span class="non-terminal"><a href="25.6.htm#fixed-pointer-declarators">fixed-pointer-declarators</a></span> <span class="terminal">,</span> <span class="non-terminal"><a href="25.6.htm#fixed-pointer-declarator">fixed-pointer-declarator</a></span> </span></span><span class="grammar-production"><span class="name"><a name="fixed-pointer-declarator"></a>fixed-pointer-declarator</span> : <span class="rhs"><span class="non-terminal"><a href="9.4.2.htm#identifier">identifier</a></span> <span class="terminal">=</span> <span class="non-terminal"><a href="25.6.htm#fixed-pointer-initializer">fixed-pointer-initializer</a></span> </span></span><span class="grammar-production"><span class="name"><a name="fixed-pointer-initializer"></a>fixed-pointer-initializer</span> : <span class="rhs"><span class="terminal">&amp;</span> <span class="non-terminal"><a href="12.4.htm#variable-reference">variable-reference</a></span> </span><span class="rhs"><span class="non-terminal"><a href="14.14.htm#expression">expression</a></span> </span></span></span><span class="locator">
     Paragraph 2</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P2S1"></a>Each <span class="non-terminal"><a href="25.6.htm#fixed-pointer-declarator">fixed-pointer-declarator</a></span> declares a local variable of the given <span class="non-terminal"><a href="25.2.htm#pointer-type">pointer-type</a></span> and initializes that local variable with the address computed by the corresponding <span class="non-terminal"><a href="25.6.htm#fixed-pointer-initializer">fixed-pointer-initializer</a></span>.</span> <span class="sentence"><span class="sentence-number">2</span> <a name="P2S2"></a>A local variable declared in a fixed statement is accessible in any <span class="non-terminal"><a href="25.6.htm#fixed-pointer-initializer">fixed-pointer-initializer</a></span>s occurring to the right of that variable's declaration, and in the <span class="non-terminal"><a href="15.htm#embedded-statement">embedded-statement</a></span> of the fixed statement.</span> <span class="sentence"><span class="sentence-number">3</span> <a name="P2S3"></a>A local variable declared by a fixed statement is considered read-only.</span> <span class="sentence"><span class="sentence-number">4</span> <a name="P2S4"></a>A compile-time error occurs if the embedded statement attempts to modify this local variable (via assignment or the ++ and  --operators) or pass it as a ref or out parameter.</span> </span><span class="locator">
     Paragraph 3</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P3S1"></a>A <span class="non-terminal"><a href="25.6.htm#fixed-pointer-initializer">fixed-pointer-initializer</a></span> can be one of the following: </span><ul><li><span class="sentence"><span class="sentence-number">2</span> <a name="P3S2"></a> The token "&amp;" followed by a <span class="non-terminal"><a href="12.4.htm#variable-reference">variable-reference</a></span> (<a href="12.3.3.htm">&#167;12.3.3</a>) to a moveable variable (<a href="25.3.htm">&#167;25.3</a>) of an unmanaged type T, provided the type T* is implicitly convertible to the pointer type given in the fixed statement.</span> <span class="sentence"><span class="sentence-number">3</span> <a name="P3S3"></a>In this case, the initializer computes the address of the given variable, and the variable is guaranteed to remain at a fixed address for the duration of the fixed statement.</span> </li><li><span class="sentence"><span class="sentence-number">4</span> <a name="P3S4"></a> An expression of an <span class="non-terminal"><a href="19.1.htm#array-type">array-type</a></span> with elements of an unmanaged type T, provided the type T* is implicitly convertible to the pointer type given in the fixed statement.</span> <span class="sentence"><span class="sentence-number">5</span> <a name="P3S5"></a>In this case, the initializer computes the address of the first element in the array, and the entire array is guaranteed to remain at a fixed address for the duration of the fixed statement.</span> <span class="sentence"><span class="sentence-number">6</span> <a name="P3S6"></a>The behavior of the fixed statement is implementation-defined if the array expression is null or if the array has zero elements.</span> </li><li><span class="sentence"><span class="sentence-number">7</span> <a name="P3S7"></a> An expression of type string, provided the type char* is implicitly convertible to the pointer type given in the fixed statement.</span> <span class="sentence"><span class="sentence-number">8</span> <a name="P3S8"></a>In this case, the initializer computes the address of the first character in the string, and the entire string is guaranteed to remain at a fixed address for the duration of the fixed statement.</span> <span class="sentence"><span class="sentence-number">9</span> <a name="P3S9"></a>The behavior of the fixed statement is implementation-defined if the string expression is null.</span> </li></ul></span><span class="locator">
     Paragraph 4</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P4S1"></a>For each address computed by a <span class="non-terminal"><a href="25.6.htm#fixed-pointer-initializer">fixed-pointer-initializer</a></span> the fixed statement ensures that the variable referenced by the address is not subject to relocation or disposal by the garbage collector for the duration of the fixed statement.</span> <span class="sentence"><span class="sentence-number">2</span> <a name="P4S2"></a>For example, if the address computed by a <span class="non-terminal"><a href="25.6.htm#fixed-pointer-initializer">fixed-pointer-initializer</a></span> references a field of an object or an element of an array instance, the fixed statement guarantees that the containing object instance is not relocated or disposed of during the lifetime of the statement.</span> </span><span class="locator">
     Paragraph 5</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P5S1"></a>It is the programmer's responsibility to ensure that pointers created by fixed statements do not survive beyond execution of those statements.</span> <span class="sentence"><span class="sentence-number">2</span> <a name="P5S2"></a>For example, when pointers created by fixed statements are passed to external APIs, it is the programmer's responsibility to ensure that the APIs retain no memory of these pointers.</span> </span><span class="locator">
     Paragraph 6</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P6S1"></a>Fixed objects may cause fragmentation of the heap (because they can't be moved).</span> <span class="sentence"><span class="sentence-number">2</span> <a name="P6S2"></a>For that reason, objects should be fixed only when absolutely necessary and then only for the shortest amount of time possible.</span> <span class="example">[Example: The example <pre class="code-example">
class Test  
{  
   static int x;  
   int y;  
   unsafe static void F(int* p) {  
      *p = 1;  
   }  
   static void Main() {  
      Test t = new Test();  
      int[] a = new int[10];  
      unsafe {  
         fixed (int* p = &amp;x) F(p);  
         fixed (int* p = &amp;t.y) F(p);  
         fixed (int* p = &amp;a[0]) F(p);  
         fixed (int* p = a) F(p);  
      }  
   }  
}  
</pre>demonstrates several uses of the fixed statement. The first statement fixes and obtains the address of a static field, the second statement fixes and obtains the address of an instance field, and the third statement fixes and obtains the address of an array element. In each case it would have been an error to use the regular &amp; operator since the variables are all classified as moveable variables. </span></span><span class="paragraph"><span class="example">The third and fourth fixed statements in the example above produce identical results. In general, for an array instance a, specifying &amp;a[0] in a fixed statement is the same as simply specifying a. </span></span><span class="paragraph"><span class="example">Here's another example of the fixed statement, this time using string: <pre class="code-example">
class Test  
{  
   static string name = "xx";  
   
   unsafe static void F(char* p) {  
      for (int i = 0; p[i] != '\0'; ++i)  
      Console.WriteLine(p[i]);  
   }  
   
   static void Main() {  
      unsafe {  
         fixed (char* p = name) F(p);  
         fixed (char* p = "xx") F(p);  
      }  
   }  
}  
</pre>end example]</span> </span><span class="locator">
     Paragraph 7</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P7S1"></a>In an unsafe context array elements of single-dimensional arrays are stored in increasing index order, starting with index 0 and ending with index Length  -1.</span> <span class="sentence"><span class="sentence-number">2</span> <a name="P7S2"></a>For multi-dimensional arrays, array elements are stored such that the indices of the rightmost dimension are increased first, then the next left dimension, and so on to the left.</span> </span><span class="locator">
     Paragraph 8</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P8S1"></a>Within a fixed statement that obtains a pointer p to an array instance a, the pointer values ranging from p to p + a.Length  -1 represent addresses of the elements in the array.</span> <span class="sentence"><span class="sentence-number">2</span> <a name="P8S2"></a>Likewise, the variables ranging from p[0] to p[a.Length  -1] represent the actual array elements.</span> <span class="sentence"><span class="sentence-number">3</span> <a name="P8S3"></a>Given the way in which arrays are stored , we can treat an array of any dimension as though it were linear.</span> <span class="example">[Example: For example. <pre class="code-example">

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -