stackalloc etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
stackalloc etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

10 Ocak 2019 Perşembe

stackalloc Anahtar Kelimesi

Giriş
Belleğin heap yerine stack'te yaratılması içindir.
Örnek
Şöyle yaparız.
static unsafe void Main(string[] args)
{
  double a1 = 1;
  double* A = stackalloc double[] { a1, 0, 0, a1, a1 };
  double* B = stackalloc double[] { a1, 0, 0, 0, 0};
}
Örnek
Şöyle yaparız
public static void StackAllocFun(int count)
{
  int[] oversized = null;
  try
  {
    Span<int> s = ((uint)count < 32) ?
      stackalloc int[count] :
      (oversized = ArrayPool<int>.Shared.Rent(count)).AsSpan(0, count);

      Populate(s);
      DoSomethingWith(s);
  }
  finally
  {
    if (oversized is not null)
    {
      ArrayPool<int>.Shared.Return(oversized);
    }
  }
}