Baza wiedzyCentrum egzaminacyjne
PLPolski
Wybierz język13

CKTIS / C#

stackalloc: odniesienie praktyczne

stackalloc: odniesienie praktyczne: stackalloc rezerwuje ciągły bufor w bieżącej ramce stosu, zwykle jako Span<T>.

Wersja: C# language; verified against C# 14 reference

Składnia

stackalloc /* grammar-specific form */

stackalloc rezerwuje ciągły bufor w bieżącej ramce stosu, zwykle jako Span.

Stosuj dla małych, krótkotrwałych buforów o kontrolowanym rozmiarze.

Duży lub zależny od niezaufanego wejścia rozmiar może przepełnić stos.

Pamięć jest nieważna po wyjściu z zakresu i może być niezerowana zależnie od formy.

Kompletny program rzeczywiście używa stackalloc; oczekiwany stdout to dokładnie 3.

Przykłady

using System;
using System.Buffers;
using System.Collections.Concurrent;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using Number = System.Int32;

Span<int> values = stackalloc[] { 1, 2, 3 }; Console.WriteLine(values.Length);

Wynik

3

Kompletny program rzeczywiście używa `stackalloc`; oczekiwany stdout to dokładnie `3`.

Źródła