如何将 C# double[] 传递给 C++需要常量 double* pArr 的函数? C++, C#
我有一个 C++ 函数,写为:
MyFunc(const double* pArray, int length);
我需要向其中传递一个非常量数组:
//C#
double[] myDoubleArray = new double[] { 1, 2, 3, 4, 5 };
MyFunc(myDoubleArray, 5);
当我执行此操作时,程序会中断。
编辑:
//C# declaration
[DllImport(@"RTATMATHLIB.dll", EntryPoint = "?MyFunc@@YANPBNHHHH@Z")]
public static extern double MyFunc(double[] data, int length);
//C# usage
public static double MyFunc(double[] data)
{
return MyFunc(data, data.Length);
}
//C++ export
__declspec(dllexport) double MyFunc(const double* data, int length);
//C++ signature
double MyFunc(const double* data, int length)
{
return 0; //does not quite matter what it returns...
}
I have a function in C++ written as:
MyFunc(const double* pArray, int length);
I need to pass a non-constant array into it:
//C#
double[] myDoubleArray = new double[] { 1, 2, 3, 4, 5 };
MyFunc(myDoubleArray, 5);
The program is breaking when I do this.
edit:
//C# declaration
[DllImport(@"RTATMATHLIB.dll", EntryPoint = "?MyFunc@@YANPBNHHHH@Z")]
public static extern double MyFunc(double[] data, int length);
//C# usage
public static double MyFunc(double[] data)
{
return MyFunc(data, data.Length);
}
//C++ export
__declspec(dllexport) double MyFunc(const double* data, int length);
//C++ signature
double MyFunc(const double* data, int length)
{
return 0; //does not quite matter what it returns...
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您始终可以将非常量数组传递给需要常量数组的函数。数组的 const 限定符意味着该函数不会修改数组的内容。在传递给函数之前,数组不需要是常量;由于声明中的 const,该函数不会修改内容
You can always pass a non-constant array to a function requiring a constant array. The const qualifier to the array implies that the function will not modify the contents of the array. The array does not need to be constant before passing to the function; the function will not modify the contents, due to the const in the declaration
可以简单地添加常量。你的程序没有错。你肯定还漏掉了一些其他的细节。
Constness can be trivially added. Your program is not wrong. You must have missed some other detail.