Alloate a chuck on the stack : stackalloc « unsafe « C# / CSharp Tutorial






You can allocate memory from the stack by using stackalloc.

stackalloc can be used only when initializing local variables.

stackalloc has this general form:

type *p = stackalloc type[size]

stackalloc must be used in an unsafe context.

using System;
using System.Globalization;

public class MainClass{

  static void Main(string[] args)
  {
      
    unsafe  
    {
      char* p = stackalloc char[256];
      for (int k = 0; k < 256; k++) 
        p[k] = (char)k;
    }  
  }
}








36.4.stackalloc
36.4.1.Alloate a chuck on the stack