C 到 C# 代码转换 - 数组作为参数
我陷入了 C 代码中的递归函数(我没有编写 C 代码)。这是我正在转换的片段:
int makeroad(float x1, float y1, float x2, float y2, float var, float X[], float Y[], float prec)
{
//stuff
k+=makeroad(x,y,x2,y2,var,X+k,Y+k,prec);
}
我不完全确定这是在做什么。这是 C 代码中唯一具有该名称的函数,因此这不是重载问题。当它递归调用自身时,它将 k 添加到 X 和 Y 数组中。将其放入 C# 中看起来像这样:
int makeroad (float x1, float y1, float x2, float y2, float var, float[] X, float[] Y, float prec)
{
//stuff
k += makeroad(x, y, x2, y2, var, X + k, Y + k, prec);
}
Visual Studio 告诉我 X + k 和 Y + k 无效。原始的 C 代码可以在 Visual C++ Express 2010 中编译并正常工作。我不认为大小写的 x 和 y 变量之间存在混淆。如果有的话,那么代码的运行纯属运气好。
有什么想法吗?
I'm stuck on a recursive function in the C code (I did not write the C code). Here is the snippet I'm converting:
int makeroad(float x1, float y1, float x2, float y2, float var, float X[], float Y[], float prec)
{
//stuff
k+=makeroad(x,y,x2,y2,var,X+k,Y+k,prec);
}
I'm not entirely sure what this is doing. This is the only function in the C code with that name, so it's not an overloading issue. When it recursively calls itself, it is adding k to the X and Y arrays. Putting it in C# looks like this:
int makeroad (float x1, float y1, float x2, float y2, float var, float[] X, float[] Y, float prec)
{
//stuff
k += makeroad(x, y, x2, y2, var, X + k, Y + k, prec);
}
And Visual Studio is telling me that the X + k and Y + k are invalid. The original C code compiles and works fine in Visual C++ Express 2010. I don't think there was confusion between the upper and lower case x and y variables respectively. If there was, the code is working by sheer luck.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 C 中,您可以通过将指针传递给第 k 个项目(即 array+k)来“创建”一个没有现有数组中前 k 个元素的数组,因为 C 中的数组是通过传递第一个元素的位置(这样的“新”数组根本不是新的,因为它引用“原始”数组的相同元素)。
在 C# 中,这不起作用,因为数组不只是作为指向连续内存的指针来处理。如果你想要快速修复,你可以创建一个新数组,使用 Array.Copy 将元素从第 k 个复制到末尾,然后将这个新数组传递给递归函数,但这是如果算法修改数组的元素,则速度很慢并且不起作用(修改不会反映在其他数组中,因为它们现在是副本)。
更好的解决方案是按原样传递数组并将 k 作为参数,并使例程从该位置开始使用数组。您应该实现相同的行为,而不会造成明显的速度损失。
In C you can "make" an array without the first k elements from an existing one just by passing the pointer to the k-th item (i.e.
array+k
), since arrays in C are passed by passing the location of their first element (such "new" array wouldn't be new at all, since it would refer to the same elements of the "original" one).In C# this doesn't work since an array isn't handled just as a pointer to contiguous memory; if you want a quick fix, you can create a new array, copy the elements from the k-th to the end with
Array.Copy
and then pass this new array to the recursive function, but this is slow and doesn't work if the algorithm modifies the elements of the array (the modifications wouldn't be reflected in the other arrays, since they are now copies).A better solution would be to pass the array as it is and
k
as a parameter, and make the routine start using the array from that position. You should achieve the same behavior without substantial speed penalties.在 C 中,数组是通过引用传递的,因此实际传递的是指向数组开头的指针。当您传递“X + k”时,您将传递一个指向从 k 开始的子数组的指针。
我不知道 C# 对数组参数做了什么,我也不想知道。
Arrays are passed by reference in C so the actual thing passed is a pointer to the beginning of the array. When you pass e.g. "X + k" you're passing a pointer to the sub-array starting at k.
I have no clue what C# does with array parameters, nor do I want to.
X + k 和 Y + k 是指向数组的指针。它允许您“跳过”被调用函数中数组开头的部分。
您可以将 C 代码与其他 CLR 代码混合,这样您就不必真正对其进行转换。
如果你确实觉得需要,你可以考虑只传递 X、Y 和 k 并重写“东西”以使用 k 作为 X 和 Y 的起始偏移量。作者在这种情况下非常有效地使用 C,这使得它变得困难移植到另一种语言。
The X + k, and Y + k are pointers into the array. It allows you to "skip over" portions of the beginning of the array in the called function.
You can mix C code with other CLR code so you don't really have to convert this.
If you really feel you need to you might consider just passing X, Y, and k and rewriting the "stuff" to use k as a beginning offset into X and Y. The author uses C very efficiently in this case and it makes it difficult to port to another language.