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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. memcpy and buffer concatenation

memcpy and buffer concatenation

Scheduled Pinned Locked Moved C / C++ / MFC
data-structures
7 Posts 4 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.
  • M Offline
    M Offline
    Madhu_Rani
    wrote on last edited by
    #1

    Hi, I am reading data from a socket (i.e Array of BYTES, lpData)... I stored the incoming data in a buffer by using memcpy( bmpBuffer,lpData, dwCount); Now I received some more data... ( data is coming continuously)... Now I want to augment the bmpBuffer with the incoming data (till the time I receive the last packet,) What function I can call to augment the coming data in existing buffer. To explain more, as we can add two strings str="Hello" + "Hi", How we can add data of two buffers

    CPalliniC _ R 3 Replies Last reply
    0
    • M Madhu_Rani

      Hi, I am reading data from a socket (i.e Array of BYTES, lpData)... I stored the incoming data in a buffer by using memcpy( bmpBuffer,lpData, dwCount); Now I received some more data... ( data is coming continuously)... Now I want to augment the bmpBuffer with the incoming data (till the time I receive the last packet,) What function I can call to augment the coming data in existing buffer. To explain more, as we can add two strings str="Hello" + "Hi", How we can add data of two buffers

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      for instance:

      char buf[100];
      const char * str1 = "Hello";
      const char * str2 = "Hi";
      char * ptr = buf;
      unsigned int len;

      len = strlen(str1);
      memcpy(ptr, str1, len);
      ptr += len;
      len = strlen(str2);
      memcpy(ptr, str2, len);
      ptr += len;

      // The two lines below just to have a NULL terminated string
      *ptr = '\0';
      ptr++;

      In a real world case, you probably want to allocate dynamically the memory buffer (and increase its size whenever it is needed). :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      In testa che avete, signor di Ceprano?

      M 1 Reply Last reply
      0
      • M Madhu_Rani

        Hi, I am reading data from a socket (i.e Array of BYTES, lpData)... I stored the incoming data in a buffer by using memcpy( bmpBuffer,lpData, dwCount); Now I received some more data... ( data is coming continuously)... Now I want to augment the bmpBuffer with the incoming data (till the time I receive the last packet,) What function I can call to augment the coming data in existing buffer. To explain more, as we can add two strings str="Hello" + "Hi", How we can add data of two buffers

        _ Offline
        _ Offline
        _Superman_
        wrote on last edited by
        #3

        You can use memcpy for concatenation. Only thing is that you must correctly specify the offset. When the first packet is received - memcpy(bmpBuffer, lpData, dwCount); Increment a count variable - dwTotalCount += dwCount; When the next packet is received - memcpy(bmpBuffer + dwTotalCount, lpData, dwCount);

        «_Superman_» I love work. It gives me something to do between weekends.

        M 1 Reply Last reply
        0
        • _ _Superman_

          You can use memcpy for concatenation. Only thing is that you must correctly specify the offset. When the first packet is received - memcpy(bmpBuffer, lpData, dwCount); Increment a count variable - dwTotalCount += dwCount; When the next packet is received - memcpy(bmpBuffer + dwTotalCount, lpData, dwCount);

          «_Superman_» I love work. It gives me something to do between weekends.

          M Offline
          M Offline
          Madhu_Rani
          wrote on last edited by
          #4

          Thanks both of you

          1 Reply Last reply
          0
          • CPalliniC CPallini

            for instance:

            char buf[100];
            const char * str1 = "Hello";
            const char * str2 = "Hi";
            char * ptr = buf;
            unsigned int len;

            len = strlen(str1);
            memcpy(ptr, str1, len);
            ptr += len;
            len = strlen(str2);
            memcpy(ptr, str2, len);
            ptr += len;

            // The two lines below just to have a NULL terminated string
            *ptr = '\0';
            ptr++;

            In a real world case, you probably want to allocate dynamically the memory buffer (and increase its size whenever it is needed). :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            M Offline
            M Offline
            Madhu_Rani
            wrote on last edited by
            #5

            YES. U are right ... BYTE* bmpBuffer=NULL; bmpBuffer=(BYTE*)GlobalAlloc(GMEM_ZEROINIT,size); memcpy( bmpBuffer,lpData, dwCount); How I may increase the size of bmpBuffer at run time? Should I create a BIG buffer (upto 3 Mb to accomodate the incomimg image) or I can manage to increase the size of buffer on request as well...

            CPalliniC 1 Reply Last reply
            0
            • M Madhu_Rani

              YES. U are right ... BYTE* bmpBuffer=NULL; bmpBuffer=(BYTE*)GlobalAlloc(GMEM_ZEROINIT,size); memcpy( bmpBuffer,lpData, dwCount); How I may increase the size of bmpBuffer at run time? Should I create a BIG buffer (upto 3 Mb to accomodate the incomimg image) or I can manage to increase the size of buffer on request as well...

              CPalliniC Offline
              CPalliniC Offline
              CPallini
              wrote on last edited by
              #6

              If you use HeapAlloc [^] function (instead of GlobalAlloc) then you have the option to use also HeapReAlloc [^] for the purpose. If you're stuck with GlobalAlloc then you have to allocate a new, bigger memory block whenever is required, copy the original block content on the newer one and then free the original block. :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              In testa che avete, signor di Ceprano?

              1 Reply Last reply
              0
              • M Madhu_Rani

                Hi, I am reading data from a socket (i.e Array of BYTES, lpData)... I stored the incoming data in a buffer by using memcpy( bmpBuffer,lpData, dwCount); Now I received some more data... ( data is coming continuously)... Now I want to augment the bmpBuffer with the incoming data (till the time I receive the last packet,) What function I can call to augment the coming data in existing buffer. To explain more, as we can add two strings str="Hello" + "Hi", How we can add data of two buffers

                R Offline
                R Offline
                Rajesh R Subramanian
                wrote on last edited by
                #7

                Hello, How are you knowing how many bytes are to be read? I've seen most people throw some random and huge number at it. You might want to try IOCtl with FIONREAD this if you aren't doing it already there:

                DWORD dwHowMuch = 0;
                IOCtl(FIONREAD, &dwHowMuch); //After this call, dwHowMuch will tell you how many bytes should be read with the Receive call.
                Receive(m_szBuffer.GetBuffer(dwHowMuch), /*other params*/);
                szBuffer.ReleaseBuffer(-1);

                //Now, szBuffer has the data received from the socket.

                Note that I'm letting CString do the gruesome stuff of memory allocation and deallocation for me (because CString already has a GetBuffer() method, and I can exploit that here). I'm not proposing this as an excellent solution, but it works and you will be free of any hand-crafted memory management that you might want to implement. I, for once, believe that CString will manage the memory much better than we do (OK, we can write something better, but what the heck... OOP is about reusability and what GetBuffer() has to offer has been good enough for me). Also note that passing a number to CString::GetBuffer() will dynamically 'adjust' the string buffer to hold at least "that much" bytes that was passed to it (mind you this does not include the terminating zero character, but here, we aren't dealing with a string anyways). This will ensure you won't run out of memory and if the previously allocated memory was good enough to hold the data to be currently received, no harm is done! I assumed that you're using MFC, but if you are not, I see your query is already answered. Also, note that you don't 'need' MFC as such if you want to just use CString.

                It is a crappy thing, but it's life -^ Carlo Pallini

                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