-1
bool NetOutputBuffer_c::SendVectorBytes ( const void * pBuf, int iLen,vector<string> &vec )
{
        BYTE * pMy = (BYTE*)pBuf;
        while ( iLen>0 && !m_bError )
        {
                int iLeft = m_iBufferSize - ( m_pBufferPtr-m_pBuffer );
                printf("iLeft is %d\n",iLeft);
                if ( iLen<=iLeft )
                {
                        printf("iLen is %d\n",iLen);
                        memcpy ( m_pBufferPtr, pMy, iLen );
                        printf("m_pBuffer is %s\n",(char*)m_pBufferPtr);
                        vec.push_back((char*)m_pBufferPtr);
                        vec.push_back("\n");
                        m_pBufferPtr += iLen;
                        break;
                }

                ResizeIf ( iLen );
        }
        return !m_bError;
}

i)In this function,passing the following arguments

     const void *pBuf -> Buffer 
     iLen ->length of the string to be pushed into the vector(gets the string from the buffer depending on the iLen(length) what we are passing)
     vec ->To push the data's in to vector(stl) depending up on the value of iLen 

ii)we are converting the void * to Byte * like this

  BYTE * pMy = (BYTE*)pBuf;

iii)ILeft is the total remaining buffer size(it doesn't cause any issues)

iv)it satisfies the following condition if(iLen <= iLeft) and goes inside the loop.

v) memcpy ( m_pBufferPtr, pMy, iLen );

      Depending on the length of ilen,it gets the data from pMy and stores it into the m_pBufferPtr...

when i'm running the program,i'm getting the output like this

iLeft is 8059
    iLen is 74
    m_pBuffer is this is my tests document number one. also checking search within phrases.



iLeft is 7965
    iLen is 8
    m_pBuffer is test two


    iLeft is 7953
    iLen is 36
    m_pBuffer is this is my tests document number two

iLeft is 7897
    iLen is 9
    m_pBuffer is test five

iLeft is 7884
    iLen is 75
    m_pBuffer is this is my tests document number five. also checking search within phrases.


iLeft is 7789
    iLen is 8
    m_pBuffer is test six

iLeft is 7777
    iLen is 36
    m_pBuffer is this is my tests document number six


iLeft is 7721
    iLen is 10
    m_pBuffer is test seven


    iLeft is 7707
    iLen is 76
    m_pBuffer is this is my tests document number seven. also checking search within phrases.�������


   iLeft is 7611
   iLen is 10
   m_pBuffer is test eight

   iLeft is 7597
   iLen is 38
   m_pBuffer is this is my tests document number eight


   iLeft is 7539
   iLen is 9
   m_pBuffer is test nine����������

  iLeft is 7526
  iLen is 75
  m_pBuffer is this is my tests document number nine. also checking search within phrases.


 For test seven and test nine , even though the iLen value was perfect,it contains some
  garbage values...

            iLeft is 7707
            iLen is 76(total 76 six characters starting from this )
            m_pBuffer is this is my tests document number seven. also checking search within
            phrases.�������


            m_pBuffer is test nine����������

Thanks & Regards, Udaya Chandran S

barryhunter
  • 20,886
  • 3
  • 30
  • 43

1 Answers1

0

I suppose your test string in *m_pBufferPtr has no teminate(escape) charecter \0 at the end. Look here for more info.

Community
  • 1
  • 1
  • It is declared as Byte *m_pBufferPtr....So while running the code,i'm getting the value from Byte * depending up on the length and pushing the data into the vector....How do we need to check terminate the string with '\0'? – user3532122 Jul 03 '14 at 10:50
  • Just add it in the end of new string. `memcpy ( m_pBufferPtr + iLen, '\0', 1);` Also It is a good to increment `iLen` by one, because all char strings in Windows should have a escape character. But it should be taken into account in other parts of the program. – nondefaultname Jul 09 '14 at 11:10
  • In case `iLen = 76`, your test string in `*pMy` also should have escape charecter. – nondefaultname Jul 09 '14 at 13:08
  • I'm using linux machine.... Assume *pMy will be having value like this...BYTE *pMy = "JohnRahulPeter";Depending on the ilen value wat i'm getting,just need to split like john,Rahul,and peter.... memcpy ( m_pBufferPtr, pMy, iLen ); Just assume (eg)iLen value is 4,it has to copy the first four character and give me the output in m_pBufferPtr as john....But it is giving me as john***** like this....Need to do some cosmetic changes inside memcpy to produce the expected output.... – user3532122 Jul 10 '14 at 12:53
  • As you said,i've added this at the end of new string .........memcpy ( m_pBufferPtr + iLen, '\0', 1); But it is throwing this warning when i'm compiling ....warning: null argument where non-null required (argument 2) and it's crashing while running the code..... – user3532122 Jul 10 '14 at 12:56
  • Sorry, it's my mistake. Not copy but set: `memset ( m_pBufferPtr + iLen, '\0', 1);` When you use `%s`in `printf()`, function should know were string ends. Without escape character `printf()` doesn’t know where it should stop reading the string. And read junk from memory. It stops if there is `0` in junk or this memory block is occupied by another data or process or something else. – nondefaultname Jul 16 '14 at 08:19