随笔 - 216  文章 - 0 评论 - 2 阅读 - 24万
< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

方式一:使用 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   青叶煮酒  阅读(409)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示