memcpy?

Discussion in 'C++' started by cylin, Sep 15, 2004.

  1. cylin

    cylin Guest

    Dear all,

    When I use memcpy second time, it seems to be no use.
    There should be two "Hello,world!" strings in this buffer.
    I don't know what's wrong?
    Please help, thanks.

    Regards,
    cylin.
    ----------------------------------------------
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    using namespace std;

    static unsigned char buffer[100];

    class CA {
    public:
    int a;
    char* sa;
    CA(int ia):a(ia),sa(NULL) {}
    };

    int main(int argc, char* argv[])
    {
    CA A(20);
    char* l_szString="Hello,world!";
    A.sa=new char[strlen(l_szString)+1];
    strcpy(A.sa,l_szString);
    memset(buffer,0,sizeof(buffer));
    memcpy(buffer,&A,sizeof(CA));
    memcpy(buffer+sizeof(CA),A.sa,strlen(A.sa)+1);
    for (int i=0;i<40;i++) {
    cout << buffer;
    }
    cout << endl << "-----Finish--------" << endl;
    return 0;
    }

    --------------------------------------------------------------------
     
    cylin, Sep 15, 2004
    #1
    1. Advertising

  2. cylin

    David Hilsee Guest

    "cylin" <> wrote in message
    news:...
    > Dear all,
    >
    > When I use memcpy second time, it seems to be no use.
    > There should be two "Hello,world!" strings in this buffer.
    > I don't know what's wrong?
    > Please help, thanks.
    >
    > Regards,
    > cylin.
    > ----------------------------------------------
    > #include <cstdio>
    > #include <cstring>
    > #include <iostream>
    > using namespace std;
    >
    > static unsigned char buffer[100];
    >
    > class CA {
    > public:
    > int a;
    > char* sa;
    > CA(int ia):a(ia),sa(NULL) {}
    > };
    >
    > int main(int argc, char* argv[])
    > {
    > CA A(20);
    > char* l_szString="Hello,world!";
    > A.sa=new char[strlen(l_szString)+1];
    > strcpy(A.sa,l_szString);
    > memset(buffer,0,sizeof(buffer));


    At this point, you have an instance of CA whose member named "sa" points to
    dynamically allocated memory containing "Hello,world!" (13 bytes, when you
    consider the null terminator). The array named "buffer" now only contains
    zeros.

    > memcpy(buffer,&A,sizeof(CA));


    This is where things get weird. You are using a low-level function to copy
    the instance of CA into the buffer, so it seems that you are trying to store
    an int and a pointer value inside of the unsigned char array buffer. No
    characters are copied. I bet this is not what you thought this line was
    doing, since you said that you expected there to be two strings in the
    buffer. Why are you doing this?

    > memcpy(buffer+sizeof(CA),A.sa,strlen(A.sa)+1);


    Now you are using a low-level function to copy the actual contents of the
    dynamically allocated memory to the unsigned char array "buffer". This is
    the first point in the program where the character sequence "Hello,world!"
    are copied into "buffer".

    > for (int i=0;i<40;i++) {
    > cout << buffer;
    > }


    Now you output a portion of "buffer", which most likely contains the integer
    value, a pointer value, and some characters. This is all pretty low-level,
    and could easily be non-portable, especially if you try to fool around with
    the contents of "buffer" in some odd way. What are you trying to do?

    --
    David Hilsee
     
    David Hilsee, Sep 15, 2004
    #2
    1. Advertising

  3. cylin

    cylin Guest

    Hi David,

    Yes, I don't know what I think about.
    There should be only one "Hello,world!".
    Because I am using "Berkeley DB" to store some objects.
    The exact way to do this is "marshalling".
    I am lazy to do "marshalling", so copy whole object and dynamically
    allocated string to a buffer.
    Then I can only save this buffer to database.
    Thanks.

    Regards,
    cylin.
     
    cylin, Sep 15, 2004
    #3
  4. cylin wrote:
    > Dear all,
    >
    > When I use memcpy second time, it seems to be no use.
    > There should be two "Hello,world!" strings in this buffer.
    > I don't know what's wrong?
    > Please help, thanks.
    >
    > Regards,
    > cylin.
    > ----------------------------------------------
    > #include <cstdio>
    > #include <cstring>
    > #include <iostream>
    > using namespace std;
    >
    > static unsigned char buffer[100];
    >
    > class CA {
    > public:
    > int a;
    > char* sa;
    > CA(int ia):a(ia),sa(NULL) {}
    > };
    >
    > int main(int argc, char* argv[])
    > {
    > CA A(20);
    > char* l_szString="Hello,world!";
    > A.sa=new char[strlen(l_szString)+1];
    > strcpy(A.sa,l_szString);
    > memset(buffer,0,sizeof(buffer));
    > memcpy(buffer,&A,sizeof(CA));

    This would copy 8 bytes (am on linux) from the address of A which
    means 4 bytes representing integer 20 and the remaining 4 bytes
    representing the pointer (only the pointer, not the value pointed by
    it). Hence, only one copy of "Hello, world!"

    > memcpy(buffer+sizeof(CA),A.sa,strlen(A.sa)+1);
    > for (int i=0;i<40;i++) {

    Here, you'll see that the first 8 bytes actually contain the value 20
    and the address of sa (in class CA)

    > cout << buffer;
    > }
    > cout << endl << "-----Finish--------" << endl;
    > return 0;
    > }
    >
    > --------------------------------------------------------------------
    >
    >
     
    Gagan Deep Singh, Sep 15, 2004
    #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:
    577
    Dave Rahardja
    Aug 7, 2003
  2. David Bradley
    Replies:
    0
    Views:
    537
    David Bradley
    Aug 7, 2003
  3. Ninan Thomas
    Replies:
    3
    Views:
    418
    Kevin Goodsell
    Aug 22, 2003
  4. Bren
    Replies:
    8
    Views:
    2,029
    Stephen Howe
    Sep 4, 2003
  5. Replies:
    13
    Views:
    17,793
    White Wolf
    Sep 18, 2003
Loading...

Share This Page