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 codeclass Test{unsafe static void Fill(int* p, int count, int value) {for (; count != 0; count--) *p++ = value;}static void Main() {int[] a = new int[100];unsafe {fixed (int* p = a) Fill(p, 100, -1);}}}a fixed statement is used to fix an array so its address can be passed to a method that takes a pointer. endexample]A char* value produced by fixing a string instance always points to a null-terminated string. Within a fixedstatement that obtains a pointer p to a string instance s, the pointer values ranging from p top + s.Length - 1 represent addresses of the characters in the string, and the pointer valuep + s.Length always points to a null character (the character with value '\0').Modifying objects of managed type through fixed pointers can result in undefined behavior. [Note: Forexample, because strings are immutable, it is the programmer’s responsibility to ensure that the charactersreferenced by a pointer to a fixed string are not modified. end note][Note: The automatic null-termination of strings is particularly convenient when calling external APIs thatexpect “C-style” strings. Note, however, that a string instance is permitted to contain null characters. If suchnull characters are present, the string will appear truncated when treated as a null-terminated char*. endnote]25.7 Stack allocationIn an unsafe context, a local variable declaration (§15.5.1) may include a stack allocation initializer, whichallocates memory from the call stack.local-variable-initializer:expressionarray-initializerstackalloc-initializerstackalloc-initializer:stackalloc unmanaged-type [ expression ]The unmanaged-type indicates the type of the items that will be stored in the newly allocated location, andthe expression indicates the number of these items. Taken together, these specify the required allocationsize. Since the size of a stack allocation cannot be negative, it is a compile-time error to specify the numberof items as a constant-expression that evaluates to a negative value.A stack allocation initializer of the form stackalloc T[E] requires T to be an unmanaged type (§25.2) andE to be an expression of type int. The construct allocates E * sizeof(T) bytes from the call stack andreturns a pointer, of type T*, to the newly allocated block. If E is a negative value, then the behavior isundefined. If E is zero, then no allocation is made, and the pointer returned is implementation-defined. Ifthere is not enough memory available to allocate a block of the given size, aSystem.StackOverflowException is thrown.The content of the newly allocated memory is undefined.Stack allocation initializers are not permitted in catch or finally blocks (§15.10).[Note: There is no way to explicitly free memory allocated using stackalloc. end note] All stackallocatedmemory blocks created during the execution of a function member are automatically discardedwhen that function member returns. [Note: This corresponds to the alloca function, an extensioncommonly found C and C++ implementations. end note]331

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

Saved successfully!

Ooh no, something went wrong!