You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The *unmanaged_type* ([§8.8](types.md#88-unmanaged-types)) indicates the type of the items that will be stored in the newly allocated location, and the *expression* indicates the number of these items. Taken together, these specify the required allocation size. Since the size of a stack allocation cannot be negative, it is a compile-time error to specify the number of items as a *constant_expression* that evaluates to a negative value.
1073
-
1074
-
If *unmanaged_type* is omitted, it is inferred from the corresponding *stackalloc_initializer_elements*. If *expression* is omitted from *stackalloc_initializer*, it is the number of *stackalloc_element_initializer*s in the corresponding *stackalloc_initializer_elements*.
1075
-
1076
-
When a *stackalloc_initializer* includes both *expression* and *stackalloc_initializer_elements* the number of elements in that *stackalloc_initializer_elements* shall match the value of *expression*.
1077
-
1078
-
A stack allocation initializer of the form stackalloc `T[E]` requires `T` to be an unmanaged type ([§8.8](types.md#88-unmanaged-types)) and `E` to be an expression implicitly convertible to type `int`. The construct allocates `E * sizeof(T)` bytes from the call stack and returns a pointer, of type `T*`, to the newly allocated block. If `E` is a negative value, then the behavior is undefined. If `E` is zero, then no allocation is made, and the pointer returned is implementation-defined. If there is not enough memory available to allocate a block of the given size, a `System.StackOverflowException` is thrown.
1079
-
1080
-
When *stackalloc_initializer_elements* is present, the *stackalloc_initializer_elelement_list* shall consist of a sequence of expressions, each having an implicit conversion to *unmanaged_type* ([§11.2](conversions.md#112-implicit-conversions)). The expressions initialize elements in the allocated memory in increasing order, starting with the element at index zero. In the absence of a *stackalloc_initializer_elements*, the content of the newly allocated memory is undefined.
1081
-
1082
-
Unlike access to arrays, access to the elements of a `stackalloc`ed block is an unsafe operation and is not range checked.
1050
+
See §stack-allocationforgeneralinformationabouttheoperator `stackalloc`. Here, theabilityofthatoperatortoresultinapointerisdiscussed.
@@ -1142,8 +1105,25 @@ All stack-allocated memory blocks created during the execution of a function mem
1142
1105
>
1143
1106
>a `stackalloc` initializerisusedinthe `IntToString` methodtoallocateabufferof 16 charactersonthestack. Thebufferisautomaticallydiscarded when the method returns.
1144
1107
>
1108
+
>Note, however, that `IntToString` canberewritteninsafemode; thatis, withoutusingpointers, asfollows:
1109
+
>
1110
+
> ```csharp
1111
+
> publicstaticstringIntToString(intvalue)
1112
+
> {
1113
+
> if (value == int.MinValue) return "-2147483648"; // this value has no positive equivalent
1114
+
>intn=value>=0?value:-value;
1115
+
>Span<char>buffer=stackallocchar[16];
1116
+
>intidx=16;
1117
+
>do
1118
+
> {
1119
+
>buffer[--idx] = (char)(n%10+'0');
1120
+
>n/=10;
1121
+
> } while (n!=0);
1122
+
>if (value<0) buffer[--idx] ='-';
1123
+
>returnbuffer.Slice(idx).ToString();
1124
+
> }
1125
+
>
1126
+
> ```
1145
1127
>*endexample*
1146
1128
1147
-
Exceptforthe `stackalloc` operator, C# providesnopredefinedconstructsfor managing non-garbage collected memory. Such services are typically provided by supporting class libraries or imported directly from the underlying operating system.
0 commit comments