13.07.2015 Views

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 25 Unsafe codecorrectly aligned for a pointer to type C, then a pointer to type A is correctly aligned for a pointer to type C.[Example: Consider the following case in which a variable having one type is accessed via a pointer to adifferent type:char c = 'A';char* pc = &c;void* pv = pc;int* pi = (int*)pv;int i = *pi; // undefined*pi = 123456; // undefinedend example]When a pointer type is converted to a pointer to byte, the result points to the lowest addressed byte of thevariable. Successive increments of the result, up to the size of the variable, yield pointers to the remainingbytes of that variable. [Example: For example, the following method displays each of the eight bytes in adouble as a hexadecimal value:using System;class Test{static void Main() {double d = 123.456e23;unsafe {byte* pb = (byte*)&d;}}}for (int i = 0; i < sizeof(double); ++i)Console.Write(" {0,2:X}", (uint)(*pb++));Console.WriteLine();Of course, the output produced depends on endianness. end example]Mappings between pointers and integers are implementation-defined. [Note: However, on 32- and 64-bitCPU architectures with a linear address space, conversions of pointers to or from integral types typicallybehave exactly like conversions of uint or ulong values, respectively, to or from those integral types. endnote]25.5 Pointers in expressionsIn an unsafe context, an expression may yield a result of a pointer type, but outside an unsafe context it is acompile-time error for an expression to be of a pointer type. In precise terms, outside an unsafe context acompile-time error occurs if any simple-name (§14.5.2), member-access (§14.5.4), invocation-expression(§14.5.5), or element-access (§14.5.6) is of a pointer type.In an unsafe context, the primary-no-array-creation-expression (§14.5) and unary-expression (§14.6)productions permit the following additional constructs:primary-no-array-creation-expression:…pointer-member-accesspointer-element-accesssizeof-expressionunary-expression:…pointer-indirection-expressionaddressof-expressionThese constructs are described in the following sections.[Note: The precedence and associativity of the unsafe operators is implied by the grammar. end note]323

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!