Skip to content
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
merged 2 commits into from
Apr 12, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

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
BillWagner committed Apr 11, 2018
commit 5c81c7739a73b437418265999f5ecb66949cef0f

This file was deleted.

86 changes: 50 additions & 36 deletions docs/csharp/language-reference/keywords/stackalloc.md
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"
Expand All @@ -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)
Copy link
Contributor

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


`stackalloc` is like [_alloca](/cpp/c-runtime-library/reference/alloca) in the C run-time library.

## Example2
Copy link
Contributor

Choose a reason for hiding this comment

The 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)
Expand Down