CKTIS / C#
stackalloc: praktična referenca
stackalloc: praktična referenca: stackalloc rezervira strnjen medpomnilnik v trenutnem okviru sklada, navadno kot Span<T>.
Različica: C# language; verified against C# 14 reference
Sintaksa
stackalloc /* grammar-specific form */stackalloc rezervira strnjen medpomnilnik v trenutnem okviru sklada, navadno kot Span.
Uporabite ga za majhne kratkotrajne medpomnilnike z nadzorovano velikostjo.
Velika velikost ali vrednost iz nezaupanja vrednega vhoda lahko preplavi sklad.
Pomnilnik ob izhodu iz obsega ni več veljaven in morda ni inicializiran.
Celoten program dejansko uporabi stackalloc; natančno pričakovani stdout je 3.
Primeri
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);
Rezultat
3
Celoten program dejansko uporabi `stackalloc`; natančno pričakovani stdout je `3`.