1

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.

Juan
  • 15,274
  • 23
  • 105
  • 187
  • 5
    Take a look to Eric Lippert's code: http://blogs.msdn.com/b/ericlippert/archive/2011/03/10/references-and-pointers-part-two.aspx Maybe his way will be helpful – Nick Martyshchenko Apr 27 '11 at 14:40
  • Why not post it as an answer? – Juan Apr 27 '11 at 15:01
  • done. Just thought quick notice is not enough for answer. – Nick Martyshchenko Apr 27 '11 at 16:36
  • 1
    Obviously I would do it the way Nick suggests. :-) However I note that it is *possible* to do it with pointers in C#, it is just a really bad idea. What you have to do is tell the garbage collector "never move this array"; you have to pin the array into one place in memory with a GCHandle, and then you can take a pointer to its interior. That is a horrid thing to do; it makes the garbage collector less efficient, and then you also have the stress of living in fear that you unpin the array while there's still a live pointer to it out there you forgot about. – Eric Lippert Apr 27 '11 at 17:45
  • Oh, and to tell the compiler that a method is unsafe, you put the word "unsafe" before the method, or before the class that contains it. You also have to click a box in the IDE that says "allow unsafe code". – Eric Lippert Apr 27 '11 at 17:46

3 Answers3

1

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.

BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
  • 1
    Wonder why they didn't implement, at least, the [] operator. Kinda pointless without it. Lazy MS programmers ;) – Juan Apr 27 '11 at 15:02
  • @jsoldi: I agree, it is a little inconvenient since it changes how you access the underlying array - I guess they either a) wanted to be very explicit about it or b) didn't think about it too much ;-) – BrokenGlass Apr 27 '11 at 15:15
1

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.

Nick Martyshchenko
  • 4,231
  • 2
  • 20
  • 24
0

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.

MattDavey
  • 8,897
  • 3
  • 31
  • 54
  • How? As I said, I can't get my `public Neuron(int* chromosomeSection)` line to compile. – Juan Apr 27 '11 at 15:29