在 C# 中将指向数组部分的指针作为参数传递

发布于 2024-11-03 08:48:04 字数 612 浏览 5 评论 0原文

我刚刚学习神经网络,我想让神经元的构造函数接收一个指向数组中作为染色体的部分的指针。像这样的东西:

public int* ChromosomeSection;

public Neuron(int* chromosomeSection)
{
    ChromosomeSection = chromosomeSection;
}

那么我会用这样的东西创建我的神经元:

int[] Chromosome = new int[neuronsCount * neuronDataSize];

for (int n = 0; n < Chromosome.Length; n += neuronDataSize)
{
    AddNeuron(new Neuron(Chromosome + n));
}

可以在 C# 中做到这一点吗?我知道 C# 支持一些不安全的代码。但我不知道如何告诉编译器该行 public Neuron(int*chromosomeSection) 是不安全的。

另外,我能够执行在 C++ 或 C 中执行的所有操作吗?在开始这样做之前我应该​​注意什么问题吗?以前从未使用过 C# 中的不安全代码。

I'm just learning neural networks and I would like to have the neuron's constructor receive a pointer to a section in an array that would be the chromosome. Something like this:

public int* ChromosomeSection;

public Neuron(int* chromosomeSection)
{
    ChromosomeSection = chromosomeSection;
}

So then I would create my neurons with something like this:

int[] Chromosome = new int[neuronsCount * neuronDataSize];

for (int n = 0; n < Chromosome.Length; n += neuronDataSize)
{
    AddNeuron(new Neuron(Chromosome + n));
}

Is it possible to do this in C#? I know C# supports some unsafe code. But I don't know how to tell the compiler that the line public Neuron(int* chromosomeSection) is unsafe.

Also, will I be able to do every operation that I would do in C++ or C? Is there any gotcha I should be aware of before starting to do it this way? Never worked with unsafe code in C# before.

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

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

发布评论

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

评论(3

記憶穿過時間隧道 2024-11-10 08:48:05

Eric Lippert 有一个不错的两部分系列:参考和指针,第一部分“托管指针”实现(参考文献和指针,第二部分)

这是我在将一些复杂的指针操作代码从 C 转换为 C# 时想到的一个方便的类型。它可以让您创建一个指向数组内部的安全“托管指针”。您可以获得对非托管指针可以执行的所有操作:您可以将其作为数组的偏移量取消引用、执行加法和减法、比较两个指针是否相等或不相等以及表示空指针。但与相应的不安全代码不同,此代码不会扰乱垃圾收集器,并且如果您做了一些愚蠢的事情(例如尝试比较不同数组内部的两个指针),则会断言。 (*) 享受吧!

希望有帮助。

Eric Lippert has nice two-part series: References and Pointers, Part One and "managed pointers" implementation (References and Pointers, Part Two).

Here's a handy type I whipped up when I was translating some complex pointer-manipulation code from C to C#. It lets you make a safe "managed pointer" to the interior of an array. You get all the operations you can do on an unmanaged pointer: you can dereference it as an offset into an array, do addition and subtraction, compare two pointers for equality or inequality, and represent a null pointer. But unlike the corresponding unsafe code, this code doesn't mess up the garbage collector and will assert if you do something foolish, like try to compare two pointers that are interior to different arrays. (*) Enjoy!

Hope it helps.

寒江雪… 2024-11-10 08:48:05

听起来你可以使用 ArraySegment对于你正在尝试做的事情。

ArraySegment 是一个包装器
一个分隔范围的数组
该数组中的元素。多种的
ArraySegment实例可以参考
相同的原始数组并且可以
重叠。

Array 属性返回整个
原始数组,而不是副本
大批;因此,对
Array 属性返回的数组
被制作到原始数组。

Sounds like you could use ArraySegment<int> for what you are trying to do.

ArraySegment is a wrapper around
an array that delimits a range of
elements in that array. Multiple
ArraySegment instances can refer to
the same original array and can
overlap.

The Array property returns the entire
original array, not a copy of the
array; therefore, changes made to the
array returned by the Array property
are made to the original array.

漫漫岁月 2024-11-10 08:48:05

是的,这在 C# 中是完全可能的,尽管单独的指针不足以以这种方式使用它,但您还需要一个 Int32 长度参数,这样您就知道在不溢出的情况下可以安全地增加该指针多少次 -如果您有 C++ 背景,这应该很熟悉。

Yes this is perfectly possible in C#, although a pointer on alone is not sufficient information to use it in this way, you'd also need an Int32 length parameter, so you know how many times it's safe to increment that pointer without an overrun - this should be familiar if you're from a C++ background.

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