13.07.2015 Views

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>C#</strong> LANGUAGE SPECIFICATION• The stackalloc operator may be used to allocate memory from the call stack (§25.7).• The fixed statement may be used to temporarily fix a variable so its address can be obtained (§25.6).25.3 Fixed and moveable variablesThe address-of operator (§25.5.4) and the fixed statement (§25.6) divide variables into two categories:Fixed variables and moveable variables.Fixed variables reside in storage locations that are unaffected by operation of the garbage collector.(Examples of fixed variables include local variables, value parameters, and variables created bydereferencing pointers.) On the other hand, moveable variables reside in storage locations that are subject torelocation or disposal by the garbage collector. (Examples of moveable variables include fields in objectsand elements of arrays.)The & operator (§25.5.4) permits the address of a fixed variable to be obtained without restrictions.However, because a moveable variable is subject to relocation or disposal by the garbage collector, theaddress of a moveable variable can only be obtained using a fixed statement (§25.6), and that addressremains valid only for the duration of that fixed statement.In precise terms, a fixed variable is one of the following:• A variable resulting from a simple-name (§14.5.2) that refers to a local variable or a value parameter.• A variable resulting from a member-access (§14.5.4) of the form V.I, where V is a fixed variable of astruct-type.• A variable resulting from a pointer-indirection-expression (§25.5.1) of the form *P, a pointer-memberaccess(§25.5.2) of the form P->I, or a pointer-element-access (§25.5.3) of the form P[E].All other variables are classified as moveable variables.A static field is classified as a moveable variable. Also, a ref or out parameter is classified as a moveablevariable, even if the argument given for the parameter is a fixed variable. Finally, a variable produced bydereferencing a pointer is always classified as a fixed variable.25.4 Pointer conversionsIn an unsafe context, the set of available implicit conversions (§13.1) is extended to include the followingimplicit pointer conversions:• From any pointer-type to the type void*.• From the null type to any pointer-type.Additionally, in an unsafe context, the set of available explicit conversions (§13.2) is extended to include thefollowing explicit pointer conversions:• From any pointer-type to any other pointer-type.• From sbyte, byte, short, ushort, int, uint, long, or ulong to any pointer-type.• From any pointer-type to sbyte, byte, short, ushort, int, uint, long, or ulong.Finally, in an unsafe context, the set of standard implicit conversions (§13.3.1) includes the followingpointer conversion:• From any pointer-type to the type void*.Conversions between two pointer types never change the actual pointer value. In other words, a conversionfrom one pointer type to another has no effect on the underlying address given by the pointer.When one pointer type is converted to another, if the resulting pointer is not correctly aligned for thepointed-to type, the behavior is undefined if the result is dereferenced. In general, the concept “correctlyaligned” is transitive: if a pointer to type A is correctly aligned for a pointer to type B, which, in turn, is322

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

Saved successfully!

Ooh no, something went wrong!