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 / C++ / MFC
  4. How to use memcpy() efficiently?

How to use memcpy() efficiently?

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialjsonquestion
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.
  • P Offline
    P Offline
    pavanbabut
    wrote on last edited by
    #1

    Hi, I would like to use memcpy() function to copy my source data into my destination data at a time rather than using for loop to go one by one. But the problem that I am not understanding is, my destination buffer size is not same as my source buffer size :confused:, but is greater than the later for sure. So, what all I want to implement is first copy all the source data into my destination buffer and again copy the source data into the rest of my destination buffer (yeh, this time the length of the copied buffer shoudl be the size of the left over buffer from the first iteration). For example- Say my source buffer = 630000 bytes, destination buffer = 630272 bytes So on fisr iteration of memcopy i would copy the whole 630000bytes of my source buffer into my destination buffer, so at this point destination buffer is left with 272bytes more room, so what I want is to copy the first 272 bytes of source buffer into the remaining destination buffer. Is there anyway to implement this using memcpy() or memmove() etc functions?? I would really appreciate if anyone can help me out in this. thanks in advance, -Pav

    E 1 Reply Last reply
    0
    • P pavanbabut

      Hi, I would like to use memcpy() function to copy my source data into my destination data at a time rather than using for loop to go one by one. But the problem that I am not understanding is, my destination buffer size is not same as my source buffer size :confused:, but is greater than the later for sure. So, what all I want to implement is first copy all the source data into my destination buffer and again copy the source data into the rest of my destination buffer (yeh, this time the length of the copied buffer shoudl be the size of the left over buffer from the first iteration). For example- Say my source buffer = 630000 bytes, destination buffer = 630272 bytes So on fisr iteration of memcopy i would copy the whole 630000bytes of my source buffer into my destination buffer, so at this point destination buffer is left with 272bytes more room, so what I want is to copy the first 272 bytes of source buffer into the remaining destination buffer. Is there anyway to implement this using memcpy() or memmove() etc functions?? I would really appreciate if anyone can help me out in this. thanks in advance, -Pav

      E Offline
      E Offline
      El Corazon
      wrote on last edited by
      #2

      pavanbabut wrote:

      Say my source buffer = 630000 bytes, destination buffer = 630272 bytes So on fisr iteration of memcopy i would copy the whole 630000bytes of my source buffer into my destination buffer, so at this point destination buffer is left with 272bytes more room, so what I want is to copy the first 272 bytes of source buffer into the remaining destination buffer. Is there anyway to implement this using memcpy() or memmove() etc functions?? I would really appreciate if anyone can help me out in this.

      I am not sure why you would want to do this... but you can add to a pointer to offset location. if you want byte offset, only add to char pointers.

      void strangecopy(char *dest, int destsize, char *source, int sourcesize)
      {
      do
      {
      if (destsizesourcesize);
      }

      _________________________ Asu no koto o ieba, tenjo de nezumi ga warau. Talk about things of tomorrow and the mice in the ceiling laugh. (Japanese Proverb) -- modified at 23:32 Thursday 6th April, 2006

      P 1 Reply Last reply
      0
      • E El Corazon

        pavanbabut wrote:

        Say my source buffer = 630000 bytes, destination buffer = 630272 bytes So on fisr iteration of memcopy i would copy the whole 630000bytes of my source buffer into my destination buffer, so at this point destination buffer is left with 272bytes more room, so what I want is to copy the first 272 bytes of source buffer into the remaining destination buffer. Is there anyway to implement this using memcpy() or memmove() etc functions?? I would really appreciate if anyone can help me out in this.

        I am not sure why you would want to do this... but you can add to a pointer to offset location. if you want byte offset, only add to char pointers.

        void strangecopy(char *dest, int destsize, char *source, int sourcesize)
        {
        do
        {
        if (destsizesourcesize);
        }

        _________________________ Asu no koto o ieba, tenjo de nezumi ga warau. Talk about things of tomorrow and the mice in the ceiling laugh. (Japanese Proverb) -- modified at 23:32 Thursday 6th April, 2006

        P Offline
        P Offline
        pavanbabut
        wrote on last edited by
        #3

        Hi, thanks for your reply. yeh, it looks strange.. hehe.. memcpy() is the only way that I can think of to get around the for loop which greatly reduces the speed in real time. Actually my destination buffers are the buffers that are allocated on a PCI board which should be in multiples of 1024 only whereas my source data is not in multiples of 1024. So, the only way I cna do this is as follows- iteration1- destination buffer1(0:629999) = srouce buffer iteration2- destination buffer1(630000:630271) = source buffer(0:271) destination buffer2(0:629727) = source buffer(272:630000) iteration3- destination buffer2(629728:630272) = source buffer(0:543) destination buffer3(0:629455) = source buffer(544:630000) ..... think the destination buffers as circular buffers. Hope now the problem is clear... Actually I haven't gone through your example, will do now after my dinner ;P thanks, -Pav

        B 1 Reply Last reply
        0
        • P pavanbabut

          Hi, thanks for your reply. yeh, it looks strange.. hehe.. memcpy() is the only way that I can think of to get around the for loop which greatly reduces the speed in real time. Actually my destination buffers are the buffers that are allocated on a PCI board which should be in multiples of 1024 only whereas my source data is not in multiples of 1024. So, the only way I cna do this is as follows- iteration1- destination buffer1(0:629999) = srouce buffer iteration2- destination buffer1(630000:630271) = source buffer(0:271) destination buffer2(0:629727) = source buffer(272:630000) iteration3- destination buffer2(629728:630272) = source buffer(0:543) destination buffer3(0:629455) = source buffer(544:630000) ..... think the destination buffers as circular buffers. Hope now the problem is clear... Actually I haven't gone through your example, will do now after my dinner ;P thanks, -Pav

          B Offline
          B Offline
          Blake Miller
          wrote on last edited by
          #4

          Typically the advantage of using one of the supplied functions, like memcpy or memmove is that they are optimized to use the best possible processor instructions. If you code the same operation as a for loop, your performance might be less. People that start writing code immediately are programmers (or hackers), people that ask questions first are Software Engineers - Graham Shanks

          P 1 Reply Last reply
          0
          • B Blake Miller

            Typically the advantage of using one of the supplied functions, like memcpy or memmove is that they are optimized to use the best possible processor instructions. If you code the same operation as a for loop, your performance might be less. People that start writing code immediately are programmers (or hackers), people that ask questions first are Software Engineers - Graham Shanks

            P Offline
            P Offline
            pavanbabut
            wrote on last edited by
            #5

            Hi, Yeh your are right, that is why I want to incorporate memcpy()/memmove() functions in my code, but as I have explained, I am not sure how to implement them efficiently in my tricky buffer allocations. I am working on this from 2 days now to get it working. It would be great if anyone can come up with generalized solution for my problem :). thanks, -Pav

            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