Results 1 to 2 of 2

Thread: Overwrite avoiding in memcpy

  1. #1

    Thread Starter
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Wink Overwrite avoiding in memcpy

    Hi all,

    First I create a dynamic buffer and set all element to zero. Then add a int value first to the buffer. Then after that int value add a string to the buffer, actually at the end of int value. But seems my code overwrite each other.

    Here is the code,

    Code:
    	char* tmpBuffer = new char[10];// Dynamic allocation
    	::memset(tmpBuffer, 0, 10);
    
    	::memcpy(tmpBuffer, &iReq, 4);
    	::memcpy(tmpBuffer, &strGetName, 6);
    How can I avoid this issue.
    “victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha

  2. #2

    Thread Starter
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Re: Overwrite avoiding in memcpy

    Ok, I go it in this way.

    Code:
    	::memcpy(&tmpBuffer[0], &iReq, 4);
    	::memcpy(&tmpBuffer[4], &strGetName, 6);
    “victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width