CKTIS / C#
stackalloc: referencia práctica
stackalloc: referencia práctica: stackalloc reserva un búfer contigu en el marco de pila actual, normalmente como Span<T>.
Versión: C# language; verified against C# 14 reference
Sintaxis
stackalloc /* grammar-specific form */stackalloc reserva un búfer contigu en el marco de pila actual, normalmente como Span.
Úsalo para búferes pequeños y breves cuyo tamaño esté controlado.
Un tamaño grande o procedente de entrada no confiable puede desbordar la pila.
La memoria deja de ser válida al salir del ámbito y puede no estar inicializada según la forma.
El programa completo usa realmente stackalloc; su stdout esperado exacto es 3.
Ejemplos
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);
Resultado
3
El programa completo usa realmente `stackalloc`; su stdout esperado exacto es `3`.