Array iteration using unsafe code
-
Consider an array of some object like ClassA. How can I quickly iterate thru this array using unsafe code and pointers? Could you please provide me a sample code?
Don't forget, that's
Persian Gulf
not Arabian gulf!
Murphy:Click Here![^] Events and Delegates simplified:Click Here![^]
I'm thirsty like sun, more landless than wind...
-
Consider an array of some object like ClassA. How can I quickly iterate thru this array using unsafe code and pointers? Could you please provide me a sample code?
Don't forget, that's
Persian Gulf
not Arabian gulf!
Murphy:Click Here![^] Events and Delegates simplified:Click Here![^]
I'm thirsty like sun, more landless than wind...
I found this code:
using System;
class App {
unsafe static void Main() {// Construct an array consisting of 5 Int32 elements Int32\[\] arr = new Int32\[\] { 1, 2, 3, 4, 5 }; // Obtain a pointer to the array's 0th element fixed (Int32\* element = &arr\[0\]) { // Iterate through each element in the array // NOTE: The code below has a bug! for (Int32 x = 0, l = arr.Length; x <= l; x++) { Console.WriteLine(element\[x\]); } }
}
}I found it here[^], which I found as the first result by googling your words: "
fast array iteration C# unsafe
". - Daniël Pelsmaeker"All e-mail I send starts with a "Hello Colin" and ends with a suitable goodbye. I get some funny looks from people who aren't called Colin, but at least it's polite." -David Wulff
-
Consider an array of some object like ClassA. How can I quickly iterate thru this array using unsafe code and pointers? Could you please provide me a sample code?
Don't forget, that's
Persian Gulf
not Arabian gulf!
Murphy:Click Here![^] Events and Delegates simplified:Click Here![^]
I'm thirsty like sun, more landless than wind...
-
I found this code:
using System;
class App {
unsafe static void Main() {// Construct an array consisting of 5 Int32 elements Int32\[\] arr = new Int32\[\] { 1, 2, 3, 4, 5 }; // Obtain a pointer to the array's 0th element fixed (Int32\* element = &arr\[0\]) { // Iterate through each element in the array // NOTE: The code below has a bug! for (Int32 x = 0, l = arr.Length; x <= l; x++) { Console.WriteLine(element\[x\]); } }
}
}I found it here[^], which I found as the first result by googling your words: "
fast array iteration C# unsafe
". - Daniël Pelsmaeker"All e-mail I send starts with a "Hello Colin" and ends with a suitable goodbye. I get some funny looks from people who aren't called Colin, but at least it's polite." -David Wulff
That's got to be the most useless code I've ever seen! Yeah, their using an unsafe context - but it's doing the same exact thing that the CLR would do in managed execution anyway. I doubt the performance payout is worth the lack of memory protection and the other problems you'll run into if this isn't run from the local machine (i.e., the MyComputer code group in reference to code access security). Note - no offense meant to you. You simply re-posted this from a simple search result.
Microsoft MVP, Visual C# My Articles
-
Consider an array of some object like ClassA. How can I quickly iterate thru this array using unsafe code and pointers? Could you please provide me a sample code?
Don't forget, that's
Persian Gulf
not Arabian gulf!
Murphy:Click Here![^] Events and Delegates simplified:Click Here![^]
I'm thirsty like sun, more landless than wind...
Is the object unmanaged? If not, it will be difficult. The GC is agnostic of pointers, so pointers are not allowed to reference types or structs that may appear (including structs with references) on the managed heap. If it were allowed, you could potentially wind up with heap resources referenced only by pointers that could be reclaimed while still in use. From the language spec: Unlike references (values of reference types), pointers are not tracked by the garbage collector - the garbage collector has no knowledge of pointers and the data to which they point. For this reason a pointer is not permitted to point to a reference or to a struct that contains references, and the referent type of a pointer must be an unmanaged-type. An unmanaged-type is any type that isn't a reference-type and doesn't contain reference-type fields at any level of nesting. In other words, an unmanaged-type is one of the following: sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal, or bool.
The most exciting phrase to hear in science, the one that heralds the most discoveries, is not 'Eureka!' ('I found it!') but 'That's funny...’