memcpy()

Discussion in 'C++' started by ishmael4, Sep 24, 2005.

  1. ishmael4

    ishmael4 Guest

    I dont know what to do with this function. It separates a block of binary
    data into [max_pkg_data] blocks, and then deals with the rest of binary (les
    than 5000 bytes).
    Here we go:

    --cut here--
    void SendBinary(binary& sent, int status)
    {
    unsigned int n=0;
    for (n=0;n<sent.size/max_pkg_data;++n)
    {
    pkg nPkg;
    memcpy(&nPkg.data[0],&sent.buffer[n*max_pkg_data],max_pkg_data); //(1)
    nPkg.info=0;
    nPkg.size=max_pkg_data;
    //debug info//
    String a(nPkg.data);
    int i=a.Length(); //(2)
    //debug info//
    Form1->SS1->Socket->Connections[0]->SendBuf((void*)&nPkg,sizeof(pkg));
    }
    pkg nPkg;
    memcpy(&nPkg.data[0],&sent.buffer[n*max_pkg_data],max_pkg_data);
    nPkg.info=status;
    nPkg.size=sent.size-((n)*max_pkg_data);
    //debug info//
    String a(nPkg.data);
    int i=a.Length(); // (3)
    //debug info//
    Form1->SS1->Socket->Connections[0]->SendBuf((void*)&nPkg,sizeof(pkg));

    return;
    }
    --cut here--

    BUT after executing (1), debug info (2) gives me size 5003. So 3 more bytes
    are copied into nPkg.data. I dont know why, esspecially that after memcpy()
    after the loop is executed, (3) gives me right size. And if I put:

    memcpy(&nPkg.data[0],&sent.buffer[n*max_pkg_data],max_pkg_data-3); //(1)

    it (2) gives me the exact size, 4997 bytes. Why?
    Please help me.

    --
    ishmael4
    ishmael4, Sep 24, 2005
    #1
    1. Advertising

  2. ishmael4 wrote:
    > I dont know what to do with this function. It separates a block of binary
    > data into [max_pkg_data] blocks, and then deals with the rest of binary (les
    > than 5000 bytes).
    > Here we go:
    >
    > --cut here--
    > void SendBinary(binary& sent, int status)
    > {
    > unsigned int n=0;
    > for (n=0;n<sent.size/max_pkg_data;++n)
    > {
    > pkg nPkg;
    > memcpy(&nPkg.data[0],&sent.buffer[n*max_pkg_data],max_pkg_data); //(1)
    > nPkg.info=0;
    > nPkg.size=max_pkg_data;
    > //debug info//
    > String a(nPkg.data);
    > int i=a.Length(); //(2)
    > //debug info//
    > Form1->SS1->Socket->Connections[0]->SendBuf((void*)&nPkg,sizeof(pkg));
    > }
    > pkg nPkg;
    > memcpy(&nPkg.data[0],&sent.buffer[n*max_pkg_data],max_pkg_data);


    This should be

    memcpy(&nPkg.data[0],&sent.buffer[n*max_pkg_data],sent.size-((n)*max_pkg_data);

    > nPkg.info=status;
    > nPkg.size=sent.size-((n)*max_pkg_data);
    > //debug info//
    > String a(nPkg.data);
    > int i=a.Length(); // (3)
    > //debug info//
    > Form1->SS1->Socket->Connections[0]->SendBuf((void*)&nPkg,sizeof(pkg));
    >
    > return;
    > }
    > --cut here--
    >
    > BUT after executing (1), debug info (2) gives me size 5003. So 3 more bytes
    > are copied into nPkg.data. I dont know why, esspecially that after memcpy()
    > after the loop is executed, (3) gives me right size. And if I put:
    >
    > memcpy(&nPkg.data[0],&sent.buffer[n*max_pkg_data],max_pkg_data-3); //(1)
    >
    > it (2) gives me the exact size, 4997 bytes. Why?
    > Please help me.
    >


    There is no such C++ type as String, perhaps you meant string?

    string expects as its constructor parameter a null terminated sequence
    of bytes. You have not given it that so any length you get from the
    string is not going to be reliable, it could be longer or it could be
    shorter.

    Maybe String is the same as string, maybe String is a mistype for
    string, who knows.

    In any case the error is in your debugging code not in your real code.

    john
    John Harrison, Sep 24, 2005
    #2
    1. Advertising

  3. ishmael4

    ishmael4 Guest

    Uzytkownik "John Harrison" <> napisal w
    wiadomosci news:vQfZe.2029$...
    > ishmael4 wrote:
    >> I dont know what to do with this function. It separates a block of binary
    >> data into [max_pkg_data] blocks, and then deals with the rest of binary
    >> (les than 5000 bytes).
    >> Here we go:
    >>
    >> --cut here--
    >> void SendBinary(binary& sent, int status)
    >> {
    >> unsigned int n=0;
    >> for (n=0;n<sent.size/max_pkg_data;++n)
    >> {
    >> pkg nPkg;
    >> memcpy(&nPkg.data[0],&sent.buffer[n*max_pkg_data],max_pkg_data); //(1)
    >> nPkg.info=0;
    >> nPkg.size=max_pkg_data;
    >> //debug info//
    >> String a(nPkg.data);
    >> int i=a.Length(); //(2)
    >> //debug info//
    >> Form1->SS1->Socket->Connections[0]->SendBuf((void*)&nPkg,sizeof(pkg));
    >> }
    >> pkg nPkg;
    >> memcpy(&nPkg.data[0],&sent.buffer[n*max_pkg_data],max_pkg_data);

    >
    > This should be
    >
    > memcpy(&nPkg.data[0],&sent.buffer[n*max_pkg_data],sent.size-((n)*max_pkg_data);


    i dont think so. if sent.size is 10023 and max_pk_data is 5000
    it would copy 10023-5000=5023 bytes.
    In second, 23 bytes, and so on.

    sent might not be a good name for this variable, it means the binary to
    send.

    >
    >> nPkg.info=status;
    >> nPkg.size=sent.size-((n)*max_pkg_data);
    >> //debug info//
    >> String a(nPkg.data);
    >> int i=a.Length(); // (3)
    >> //debug info//
    >> Form1->SS1->Socket->Connections[0]->SendBuf((void*)&nPkg,sizeof(pkg));
    >>
    >> return;
    >> }
    >> --cut here--
    >>
    >> BUT after executing (1), debug info (2) gives me size 5003. So 3 more
    >> bytes are copied into nPkg.data. I dont know why, esspecially that after
    >> memcpy() after the loop is executed, (3) gives me right size. And if I
    >> put:
    >>
    >> memcpy(&nPkg.data[0],&sent.buffer[n*max_pkg_data],max_pkg_data-3);
    >> //(1)
    >>
    >> it (2) gives me the exact size, 4997 bytes. Why?
    >> Please help me.
    >>

    >
    > There is no such C++ type as String, perhaps you meant string?
    >
    > string expects as its constructor parameter a null terminated sequence of
    > bytes. You have not given it that so any length you get from the string is
    > not going to be reliable, it could be longer or it could be shorter.
    >
    > Maybe String is the same as string, maybe String is a mistype for string,
    > who knows.


    AnsiString, which is used by BCB6.

    >
    > In any case the error is in your debugging code not in your real code.
    >
    > john


    ishmael4
    ishmael4, Sep 24, 2005
    #3
  4. ishmael4

    Mike Wahler Guest

    "ishmael4" <> wrote in message
    news:dh429t$8fu$...
    > Uzytkownik "John Harrison" <> napisal w
    >>
    >> Maybe String is the same as string, maybe String is a mistype for string,
    >> who knows.

    >
    > AnsiString, which is used by BCB6.


    I strongly suggest you use standard C++ library types (in this
    case 'std::string'), then everyone knows what is what. There's
    really no reason to use a nonstandard type such as 'AnsiString',
    and such entities are not topical here anyway.


    -Mike
    Mike Wahler, Sep 24, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Edwards
    Replies:
    1
    Views:
    567
    Dave Rahardja
    Aug 7, 2003
  2. David Bradley
    Replies:
    0
    Views:
    524
    David Bradley
    Aug 7, 2003
  3. Ninan Thomas
    Replies:
    3
    Views:
    405
    Kevin Goodsell
    Aug 22, 2003
  4. Bren
    Replies:
    8
    Views:
    2,008
    Stephen Howe
    Sep 4, 2003
  5. Replies:
    13
    Views:
    17,755
    White Wolf
    Sep 18, 2003
Loading...

Share This Page