CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Apr 2001
    Posts
    1,029

    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!

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

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

  3. #3
    Join Date
    Apr 2001
    Posts
    1,029

    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!

  4. #4
    Join Date
    Nov 2006
    Posts
    1,611

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

  5. #5
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: struct to char* to string and back to struct.. what is wrong here?

    Quote Originally Posted by JVene View Post
    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

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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&#37; certain that compiling it a different way won't break it.

  7. #7
    Join Date
    May 2002
    Posts
    1,435

    Re: struct to char* to string and back to struct.. what is wrong here?

    See JVene's comments - they are right on target.

  8. #8
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Question Re: struct to char* to string and back to struct.. what is wrong here?

    Quote Originally Posted by lab1 View Post
    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.

  9. #9
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    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
  •  





Click Here to Expand Forum to Full Width

Featured