이 제품은 선택한 Datadog 사이트에서 지원되지 않습니다. ().
이 페이지는 아직 한국어로 제공되지 않습니다. 번역 작업 중입니다.
현재 번역 프로젝트에 대한 질문이나 피드백이 있으신 경우 언제든지 연락주시기 바랍니다.

Metadata

ID: csharp-best-practices/loop-stackalloc

Language: C#

Severity: Warning

Category: Performance

Description

This rule warns against using stackalloc inside loops. Allocating memory on the stack in each iteration can lead to excessive stack usage, which may cause stack overflow exceptions and degrade application performance. To avoid this issue, allocate the stack memory once outside the loop and reuse it within the loop body.

Non-Compliant Code Examples

for (int i = 0; i < 10; i++)
{
    Span<byte> buffer = stackalloc byte[256];
    buffer.Clear();
}

Compliant Code Examples

Span<byte> buffer = stackalloc byte[256];
for (int i = 0; i < 10; i++)
{
    buffer.Clear();
}
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

원활한 통합. Datadog Code Security를 경험해 보세요