memcpy?

C

cylin

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;
}

--------------------------------------------------------------------
 
D

David Hilsee

cylin said:
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?
 
C

cylin

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.
 
G

Gagan Deep Singh

cylin said:
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;
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top