Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Array iteration using unsafe code

Array iteration using unsafe code

Scheduled Pinned Locked Moved C#
questioncsharpcomdata-structures
5 Posts 5 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Meysam Mahfouzi
    wrote on last edited by
    #1

    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...

    U S T 3 Replies Last reply
    0
    • M Meysam Mahfouzi

      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...

      U Offline
      U Offline
      User 260964
      wrote on last edited by
      #2

      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

      H 1 Reply Last reply
      0
      • M Meysam Mahfouzi

        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...

        S Offline
        S Offline
        scadaguy
        wrote on last edited by
        #3

        If your array contains objects then the best thing to use is a for loop. Unsafe code isn't going to help much.

        1 Reply Last reply
        0
        • U User 260964

          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

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • M Meysam Mahfouzi

            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...

            T Offline
            T Offline
            turbochimp
            wrote on last edited by
            #5

            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...’

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups