-
Notifications
You must be signed in to change notification settings - Fork 5.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
update stackalloc reference #4922
Merged
BillWagner
merged 2 commits into
dotnet:master
from
BillWagner:csharp73-stackalloc-iniitalizers
Apr 12, 2018
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
update stackalloc reference
Starting with C# 7.3, stackalloc arrays can use array initializer syntax. I also ran acrolinx and fixed markdown lint issues. This will be easier to review with the rich diff.
- Loading branch information
commit 5c81c7739a73b437418265999f5ecb66949cef0f
There are no files selected for viewing
47 changes: 0 additions & 47 deletions
47
docs/csharp/language-reference/keywords/codesnippet/CSharp/stackalloc_1.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
--- | ||
title: "stackalloc (C# Reference)" | ||
ms.date: 07/20/2015 | ||
ms.date: 04/12/2018 | ||
ms.prod: .net | ||
ms.technology: | ||
- "devlang-csharp" | ||
|
@@ -10,45 +10,59 @@ f1_keywords: | |
- "stackalloc" | ||
helpviewer_keywords: | ||
- "stackalloc keyword [C#]" | ||
ms.assetid: adc04c28-3ed2-4326-807a-7545df92b852 | ||
caps.latest.revision: 27 | ||
author: "BillWagner" | ||
ms.author: "wiwagn" | ||
--- | ||
# stackalloc (C# Reference) | ||
The `stackalloc` keyword is used in an unsafe code context to allocate a block of memory on the stack. | ||
|
||
```csharp | ||
int* block = stackalloc int[100]; | ||
``` | ||
|
||
## Remarks | ||
The keyword is valid only in local variable initializers. The following code causes compiler errors. | ||
|
||
```csharp | ||
int* block; | ||
// The following assignment statement causes compiler errors. You | ||
// can use stackalloc only when declaring and initializing a local | ||
// variable. | ||
block = stackalloc int[100]; | ||
``` | ||
|
||
Because pointer types are involved, `stackalloc` requires [unsafe](../../../csharp/language-reference/keywords/unsafe.md) context. For more information, see [Unsafe Code and Pointers](../../../csharp/programming-guide/unsafe-code-pointers/index.md). | ||
|
||
`stackalloc` is like [_alloca](/cpp/c-runtime-library/reference/alloca) in the C run-time library. | ||
|
||
The following example calculates and displays the first 20 numbers in the Fibonacci sequence. Each number is the sum of the previous two numbers. In the code, a block of memory of sufficient size to contain 20 elements of type `int` is allocated on the stack, not the heap. The address of the block is stored in the pointer `fib`. This memory is not subject to garbage collection and therefore does not have to be pinned (by using [fixed](../../../csharp/language-reference/keywords/fixed-statement.md)). The lifetime of the memory block is limited to the lifetime of the method that defines it. You cannot free the memory before the method returns. | ||
|
||
## Example | ||
[!code-csharp[csrefKeywordsOperator#15](../../../csharp/language-reference/keywords/codesnippet/CSharp/stackalloc_1.cs)] | ||
|
||
## Security | ||
Unsafe code is less secure than safe alternatives. However, the use of `stackalloc` automatically enables buffer overrun detection features in the common language runtime (CLR). If a buffer overrun is detected, the process is terminated as quickly as possible to minimize the chance that malicious code is executed. | ||
|
||
## C# Language Specification | ||
[!INCLUDE[CSharplangspec](~/includes/csharplangspec-md.md)] | ||
|
||
## See Also | ||
The `stackalloc` keyword is used in an unsafe code context to allocate a block of memory on the stack. | ||
|
||
```csharp | ||
int* block = stackalloc int[100]; | ||
``` | ||
|
||
## Remarks | ||
|
||
The keyword is valid only in local variable initializers. The following code causes compiler errors. | ||
|
||
```csharp | ||
int* block; | ||
// The following assignment statement causes compiler errors. You | ||
// can use stackalloc only when declaring and initializing a local | ||
// variable. | ||
block = stackalloc int[100]; | ||
``` | ||
|
||
Beginning with C# 7.3, you can use array initializer syntax for `stackalloc` arrays. All the following declarations declare an array with three elements whose values are the integers `1`, `2`, and `3`: | ||
|
||
```csharp | ||
// Valid starting with C# 7.3 | ||
int* first = stackalloc int[3] { 1, 2, 3 }; | ||
int* second = stackalloc int[] { 1, 2, 3 }; | ||
int* third = stackalloc[] { 1, 2, 3 }; | ||
``` | ||
|
||
Because pointer types are involved, `stackalloc` requires [unsafe](unsafe.md) context. For more information, see [Unsafe Code and Pointers](../../programming-guide/unsafe-code-pointers/index.md) | ||
|
||
`stackalloc` is like [_alloca](/cpp/c-runtime-library/reference/alloca) in the C run-time library. | ||
|
||
## Example2 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As the section contains two examples: Example2 -> Examples |
||
|
||
The following example calculates and displays the first 20 numbers in the Fibonacci sequence. Each number is the sum of the previous two numbers. In the code, a block of memory of sufficient size to contain 20 elements of type `int` is allocated on the stack, not the heap. The address of the block is stored in the pointer `fib`. This memory is not subject to garbage collection and therefore does not have to be pinned (by using [fixed](fixed-statement.md)). The lifetime of the memory block is limited to the lifetime of the method that defines it. You cannot free the memory before the method returns. | ||
|
||
[!code-csharp[csrefKeywordsOperator#15](../../../../samples/snippets/csharp/keywords/StackAllocExamples.cs#1)] | ||
|
||
The following example initializes a `stackalloc` array of integers to a bit mask with one bit set in each element. This demonstrates the new initializer syntax available starting in C# 7.3: | ||
|
||
[!code-csharp[csrefKeywordsOperator#15](../../../../samples/snippets/csharp/keywords/StackAllocExamples.cs#2)] | ||
|
||
## Security | ||
|
||
Unsafe code is less secure than safe alternatives. However, the use of `stackalloc` automatically enables buffer overrun detection features in the common language runtime (CLR). If a buffer overrun is detected, the process is terminated as quickly as possible to minimize the chance that malicious code is executed. | ||
|
||
## C# Language Specification | ||
[!INCLUDE[CSharplangspec](~/includes/csharplangspec-md.md)] | ||
|
||
## See Also | ||
[C# Reference](../../../csharp/language-reference/index.md) | ||
[C# Programming Guide](../../../csharp/programming-guide/index.md) | ||
[C# Keywords](../../../csharp/language-reference/keywords/index.md) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
requires unsafe --> requires an unsafe