-
April 29th, 2009, 07:36 AM
#1
struct to char* to string and back to struct.. what is wrong here?
Hello,
I wrote some code to convert a struct into a char* and then back to a struct again like this:
Code:
struct C
{
int test1;
int test2;
} myStruct, myStruct2;
myStruct.test1=1;
myStruct.test2=2;
char* test = (char*)&myStruct;
memcpy((char*)(&myStruct2), test, sizeof(myStruct2));
cout << "test 1: " << myStruct2.test1 << endl;
cout << "test 2: " << myStruct2.test2 << endl;
This works fine as the output is:
test1: 1
test2: 2
However, when I try to add a string in there, I get different output:
Code:
myStruct.test1=1;
myStruct.test2=2;
char* test = (char*)&myStruct;
string test2 = test;
memcpy((char*)(&myStruct2), test2.data(), sizeof(myStruct2));
cout << "test 1: " << myStruct2.test1 << endl;
cout << "test 2: " << myStruct2.test2 << endl;
This outputs:
test1: -859045887
test2: -858993460
My question is.. what am I doing wrong with the string? How can I make this example work with a string?
Thanks!
-
April 29th, 2009, 07:44 AM
#2
Re: struct to char* to string and back to struct.. what is wrong here?
If you put any type into the struct where the data isn't completely defined inline in the type----anything involving pointers, any non-POD C++ class----then you can't use memcpy, because the state of the object is no longer entirely defined by those few bytes within the object itself.
So the answer is: You can't make it work. Once you've casted to char*, you lose the information necessary to successfully copy the entire object state.
If you want to send it over a network socket or something, you'll have to explicitly write serialization and deserialization routines. These are often implemented via operator>> and operator <<.
-
April 29th, 2009, 08:03 AM
#3
Re: struct to char* to string and back to struct.. what is wrong here?
Actually I got it to work like this:
Code:
myStruct.test1=1;
myStruct.test2=2;
string test2((char*)&myStruct, sizeof(myStruct));
memcpy((char*)(&myStruct2), test2.data(), sizeof(myStruct2));
cout << "test 1: " << myStruct2.test1 << endl;
cout << "test 2: " << myStruct2.test2 << endl;
Thanks!
-
April 29th, 2009, 08:36 AM
#4
Re: struct to char* to string and back to struct.. what is wrong here?
lab1:
Your thought process is off track here.
You should deploy more standard copy methods than this. If you're thinking that you're enhancing performance this way, you're playing with fire and will probably get burned.
I know, it's tempting to think it should work.
What you're doing is using a 'C' oriented approach to C++, rejecting C++ concepts on the subject of copying objects in the process. I assume your motivation is on the general subject of performance, but I can assure you that you're on the wrong track.
Stick with copy constructors, overload of operator "=", and work within the tenets of good C++ practices. The results are far better.
If my post was interesting or helpful, perhaps you would consider clicking the 'rate this post' to let me know (middle icon of the group in the upper right of the post).
-
April 29th, 2009, 08:56 AM
#5
Re: struct to char* to string and back to struct.. what is wrong here?
Originally Posted by JVene
Stick with copy constructors, overload of operator "=", and work within the tenets of good C++ practices. The results are far better.
That's got my vote too
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
April 29th, 2009, 09:13 AM
#6
Re: struct to char* to string and back to struct.. what is wrong here?
"It seems to work" is pretty terrible as a justification for doing something. It could work on one system and break on another. It's not good code unless you know why it works, in very specific terms, and can be 100% certain that compiling it a different way won't break it.
-
April 29th, 2009, 12:48 PM
#7
Re: struct to char* to string and back to struct.. what is wrong here?
See JVene's comments - they are right on target.
-
April 29th, 2009, 01:33 PM
#8
Re: struct to char* to string and back to struct.. what is wrong here?
Originally Posted by lab1
Actually I got it to work like this:
Code:
myStruct.test1=1;
myStruct.test2=2;
string test2((char*)&myStruct, sizeof(myStruct));
memcpy((char*)(&myStruct2), test2.data(), sizeof(myStruct2));
cout << "test 1: " << myStruct2.test1 << endl;
cout << "test 2: " << myStruct2.test2 << endl;
Thanks!
You got what to work? This is a bizarre thread. Looks like you found an extremely bizarre and unnecessarily complex way of copying data from one object to another.
-
April 29th, 2009, 01:38 PM
#9
Re: struct to char* to string and back to struct.. what is wrong here?
The funny thing is he posted the same question a few weeks ago and got pretty much the same responses.
http://www.codeguru.com/forum/showthread.php?t=475059
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|