Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   memcpy? (http://www.velocityreviews.com/forums/t285622-memcpy.html)

cylin 09-15-2004 01:55 AM

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

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



David Hilsee 09-15-2004 02:12 AM

Re: memcpy?
 
"cylin" <cylin@avant.com.tw> wrote in message
news:2qpljnF128b2jU1@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



cylin 09-15-2004 03:05 AM

Re: memcpy?
 
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.



Gagan Deep Singh 09-15-2004 07:07 AM

Re: memcpy?
 
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;
> }
>
> --------------------------------------------------------------------
>
>



All times are GMT. The time now is 04:13 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.