471,071 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,071 software developers and data experts.

memcpy?

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[i];
}
cout << endl << "-----Finish--------" << endl;
return 0;
}

--------------------------------------------------------------------
Jul 22 '05 #1
3 2695
"cylin" <cy***@avant.com.tw> wrote in message
news:2q*************@uni-berlin.de...
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[i];
}


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
Jul 22 '05 #2
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.
Jul 22 '05 #3
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[i];
}
cout << endl << "-----Finish--------" << endl;
return 0;
}

--------------------------------------------------------------------

Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by franky.backeljauw | last post: by
16 posts views Thread by Delali Dzirasa | last post: by
6 posts views Thread by Samee Zahur | last post: by
5 posts views Thread by manya | last post: by
35 posts views Thread by Christopher Benson-Manica | last post: by
16 posts views Thread by Amarendra GODBOLE | last post: by
33 posts views Thread by Case | last post: by
6 posts views Thread by myhotline | last post: by
18 posts views Thread by Mark | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.