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. Dividing array into sub-arrays

Dividing array into sub-arrays

Scheduled Pinned Locked Moved C#
data-structureshelpquestion
5 Posts 3 Posters 0 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.
  • S Offline
    S Offline
    SebbaP
    wrote on last edited by
    #1

    Hi, I am trying to pass multiple sections of one big byte[] into a function that takes byte[] as a parameter. This is what I am trying to do: method( bigBuffer[ bufferIndex ], bufferSize ) where method is declared as void method( byte[], int ) The code above results in a compilation error because a byte cannot be converted into a byte[]. Is there a way to do this without forcing me to break the bigBuffer down into smaller byte[] byte by byte? Thanks!

    D L 2 Replies Last reply
    0
    • S SebbaP

      Hi, I am trying to pass multiple sections of one big byte[] into a function that takes byte[] as a parameter. This is what I am trying to do: method( bigBuffer[ bufferIndex ], bufferSize ) where method is declared as void method( byte[], int ) The code above results in a compilation error because a byte cannot be converted into a byte[]. Is there a way to do this without forcing me to break the bigBuffer down into smaller byte[] byte by byte? Thanks!

      D Offline
      D Offline
      Dennis C Dietrich
      wrote on last edited by
      #2

      SebbaP wrote: I am trying to pass multiple sections of one big byte[] into a function that takes byte[] as a parameter. First change the signature of the method you want to call in something like this...

      void Foo(byte[] byteArray, int offset, int length)

      ...where offset is the starting index and length the number of bytes to process. You can then call it like this:

      Foo(bigBuffer, offset, length);

      If you can't (or don't want to) change the function that is going to process the sections of your array you might use the static method Array.Copy[^]. However (as the name implies) it copies (a part) of the array and therefore is slower and the callee will not be able to manipulate the original data. Best regards Dennis

      S 1 Reply Last reply
      0
      • D Dennis C Dietrich

        SebbaP wrote: I am trying to pass multiple sections of one big byte[] into a function that takes byte[] as a parameter. First change the signature of the method you want to call in something like this...

        void Foo(byte[] byteArray, int offset, int length)

        ...where offset is the starting index and length the number of bytes to process. You can then call it like this:

        Foo(bigBuffer, offset, length);

        If you can't (or don't want to) change the function that is going to process the sections of your array you might use the static method Array.Copy[^]. However (as the name implies) it copies (a part) of the array and therefore is slower and the callee will not be able to manipulate the original data. Best regards Dennis

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

        Thank you Dennis! I cannot (or rather, would not like to) change the method signature, and as you mentioned, copying each element is slow. But that is what I have settled for (I would end up copying each element in the partitioned byte[] anyway inside Foo), until I - or someone else for that matter - comes up with a quicker solution. Anyone? Thanks!

        D 1 Reply Last reply
        0
        • S SebbaP

          Thank you Dennis! I cannot (or rather, would not like to) change the method signature, and as you mentioned, copying each element is slow. But that is what I have settled for (I would end up copying each element in the partitioned byte[] anyway inside Foo), until I - or someone else for that matter - comes up with a quicker solution. Anyone? Thanks!

          D Offline
          D Offline
          Dennis C Dietrich
          wrote on last edited by
          #4

          SebbaP wrote: I cannot (or rather, would not like to) change the method signature, and as you mentioned, copying each element is slow. But that is what I have settled for (I would end up copying each element in the partitioned byte[] anyway inside Foo), until I - or someone else for that matter - comes up with a quicker solution. Copying is slower but that does not necessarily mean that it's too slow for you. Although I'd still suggest that you should consider refactoring the method you want to call, you should give Array.Copy() a try. Somehow I doubt that it copies each element. I would rather expect a simple memcopy which should not get you into trouble unless you want to copy a few gigabytes per second. If you take a look at how Array.Copy() works you'll find out that it is implemented directly in the CLR. The method is overloaded however it always results in a call of the following method:

          [MethodImpl(MethodImplOptions.InternalCall),
          ReliabilityContract(Consistency.MayCorruptInstance, CER.MayFail)]
          internal static extern void Copy(Array sourceArray, int sourceIndex,
          Array destinationArray, int destinationIndex,
          int length, bool reliable);

          Best regards Dennis

          1 Reply Last reply
          0
          • S SebbaP

            Hi, I am trying to pass multiple sections of one big byte[] into a function that takes byte[] as a parameter. This is what I am trying to do: method( bigBuffer[ bufferIndex ], bufferSize ) where method is declared as void method( byte[], int ) The code above results in a compilation error because a byte cannot be converted into a byte[]. Is there a way to do this without forcing me to break the bigBuffer down into smaller byte[] byte by byte? Thanks!

            L Offline
            L Offline
            leppie
            wrote on last edited by
            #5

            2 more ways: 1. use the second variable as an index instead, and define a constant buffersize. 2. use a static field to store offset info. top secret
            Download xacc-ide 0.0.3 now!
            See some screenshots

            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