方式一:使用 stackalloc 关键字

int* block = stackalloc int[100];  

注:此关键字仅在局部变量初始值设定项中有效。 以下代码导致编译器错误。
int* block;  
// The following assignment statement causes compiler errors. You  
// can use stackalloc only when declaring and initializing a local   
// variable.  
block = stackalloc int[100];  
由于涉及指针类型,因此 stackalloc 需要 unsafe 下文。


方式二:使用 Marshal
IntPtr hglobal = Marshal.AllocHGlobal(100);
Marshal.FreeHGlobal(hglobal);
 

posted on 2018-02-08 13:27  青叶煮酒  阅读(392)  评论(0编辑  收藏  举报