double* 和 double** 是 blittable 类型吗? C#

发布于 2024-08-12 01:28:06 字数 303 浏览 7 评论 0原文

我有一个关于将 C++ 数组编组到 C# 的问题。 double* 会自动转换为 double[] 吗?

我知道 double 是 blittable 类型,因此 C++ 中的 double 与 C# 中的 double 相同。 那么 double** 呢,它会转换为 double[,] 吗?

我有以下非托管函数: int get_values(double** param,int sz)

其中 param 是指向双精度数组的指针,sz 是其大小。

我怎样才能将此函数DLLImport到C#?

提前致谢

I have a question regarding marshalling of C++ arrays to C#.
Does the double* automatically convert to double[]?

I know double is a blittable type, so double from C++ is the same as double from C#.
And what about double**, does it convert to double[,] ?

I have the following unmanaged function:
int get_values(double** param,int sz)

where param is a pointer to array of doubles and sz it's size.

How can I DLLImport this function to C#?

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

世界等同你 2024-08-19 01:28:06

该声明毫无意义。如果函数采用指向双精度数组的指针,则有意义,但声明将是

int get_values(double* array, int size);

其中 size 将给出客户端分配的数组的大小,并且函数的返回值指示实际将多少双精度复制到数组中。等效的 P/Invoke 声明是:

[DllImport("blah.dll")]
private static extern int get_values(double[] array, int size);

在调用函数之前,您必须使用 new 分配数组到您承诺的大小。

但 double** 参数是一个难题。这可能意味着该函数返回一个指向双精度数组的指针,但大小参数就没有意义了。因为它是拥有数组并控制其大小的函数。或者这可能意味着客户端传递一个二维数组,但只有一个大小参数是没有意义的。

请用对该函数功能的正确解释来更新您的问题。

The declaration makes no sense. It would make sense if the function takes a pointer to an array of doubles, but then the declaration would be

int get_values(double* array, int size);

Where size would give the size of the array allocated by the client and the function's return value indicates how many doubles were actually copied into the array. The equivalent P/Invoke declaration would be:

[DllImport("blah.dll")]
private static extern int get_values(double[] array, int size);

You have to allocate the array with new to the size you promised before calling the function.

But the double** argument is a hangup. It could mean that the function returns a pointer to an array of doubles but then the size argument makes little sense. Since it is the function that owns the array and controls its size. Or it could mean that the client passes a two-dimensional array, but then having only one size argument makes no sense.

Please update your question with the correct interpretation of what the function does.

姜生凉生 2024-08-19 01:28:06

本文内容如下:

以下复杂类型也是
blittable 类型:

blittable 的一维数组
类型,例如整数数组。
但是,包含一个类型
blittable 类型的变量数组是
本身不可直接传送。

This article says the following:

The following complex types are also
blittable types:

One-dimensional arrays of blittable
types, such as an array of integers.
However, a type that contains a
variable array of blittable types is
not itself blittable.

dawn曙光 2024-08-19 01:28:06

在函数参数类型的上下文中,double*double[] 是相同的:

void f(double* p);
void g(double p[]);
void h(double p[10]); // even if you supply a size! (it's ignored)

void i(double (&a)[10]); // now this is different

在其他上下文中它们不是:

void j() {
  double a[10]; // sizeof a == sizeof(double) * 10
  double* p; // sizeof p == sizeof(void*)
}

这对于 double** 也同样适用double*[],但请注意 double[][] (不带大小)是不同的,带大小意味着指向数组的指针(您必须指定除一维之外的所有维度)。

听起来确实像是您在不需要时使用了 double** 。仅将数组作为 double* 传递对您有用吗?

int get_values(double* array, int size);

void func() {
  double a[10];
  get_values(a, 10);
}

(我知道我没有回答您的 DLLImport 问题,我不知道答案。)

In the context of a function's parameter type, double* and double[] are identical:

void f(double* p);
void g(double p[]);
void h(double p[10]); // even if you supply a size! (it's ignored)

void i(double (&a)[10]); // now this is different

In other contexts they aren't:

void j() {
  double a[10]; // sizeof a == sizeof(double) * 10
  double* p; // sizeof p == sizeof(void*)
}

This holds similarly for double** vs double*[], but note double[][] (without sizes) is different, and with sizes means a pointer-to-array (you have to specify all but one dimension).

It does sound like you're using a double** when it's not needed. Does just passing the array as a double* work for you?

int get_values(double* array, int size);

void func() {
  double a[10];
  get_values(a, 10);
}

(I know I didn't answer your DLLImport question, I don't know the answer.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文