Pointers in C# to make int array?

后端 未结 3 786
感情败类
感情败类 2021-01-12 02:20

The following C++ program compiles and runs as expected:

#include 

int main(int argc, char* argv[])
{
    int* test = new int[10];

    for (         


        
3条回答
  •  清歌不尽
    2021-01-12 03:13

    You need to pin the array using the fixed keyword so it won't get moved by the GC:

    fixed (int* test = new int[10])
    {
        // ...
    }
    

    However, unsafe code in C# is more the exception than the rule. I'd try to translate your C code to non-unsafe C# code.

提交回复
热议问题