2

I just discovered reinterpret_cast in C++ and I am trying to learn more about it. I wrote this code:

struct Human{
    string name;
    char gender;
    int age;
    Human(string n, char g, int a) : name(n), gender(g), age(a) {}
};

int main()
{
    Human h("John", 'M', 26);
    char* s = reinterpret_cast<char*>(&h);
    Human *hh = reinterpret_cast<Human*>(s);
    cout << hh->name << " " << hh->gender << " " << hh->age << endl;
}

It works pretty well, exactly as expected. Now I want convert the char * to an std::string and then from this string get back the Human object:

int main()
{
    Human h("John", 'M', 26);
    char* s = reinterpret_cast<char*>(&h);

    string str = s;
    Human *hh = reinterpret_cast<Human*>(&str);

    cout << hh->name << " " << hh->gender << " " << hh->age << endl; // prints wrong values
}

Does anyone have an idea to overcome this ?

Thank you.

6
  • 2
    Although there are ways to make this work, this smells funny. In fact, this smells like an meta.stackexchange.com/questions/66377/what-is-the-xy-problem . So, what is the real problem you think you need to solve using this approach? Feb 27, 2016 at 16:24
  • That first example is a gross violation of the strict aliasing rule. Feb 27, 2016 at 16:24
  • 1
    Well, as it's explained in the linked article, a "char *" is exempt from aliasing. Even if this is actually OK, it's still not recommended, though. Feb 27, 2016 at 16:28
  • 1
    @A.Omar Ah, then yes you can convert a pointer to an object to another pointer. But, and this is important, trying to send anything complex over a socket will in almost all cases not work as expected. Take the normal std::string for example. A std::string object doesn't actually contain an actual string, it contains a pointer to a string, and sending a std::string object will send the pointer and not the data (string) it points to. In the other process, that pointer you receive is not going to point to the same thing (if at any valid memory at all). Feb 27, 2016 at 16:40
  • 1
    @A.Omar: While this can theoretically work sometimes it's not a good idea. Firstly your structs cannot contain a single pointer (or a pointer in an object) because it makes no sense. Additionally numerous things such as bitness, type size, packing, compiler, compiler version and endianness differences can change the way a struct is laid out in memory across platforms. If you need to serialize an object - just write it byte by byte in a known format. Feb 27, 2016 at 16:43

1 Answer 1

1

In your second program when you do

string str = s;

you create a completely new object that is totally unrelated to the pointer s. Getting the address from str will give you a pointer to str, and not the "string" it contains.

Also, using reinterpret_cast is a way to tell the compiler "I know what I am doing", and if you don't actually know what's happening then you will undoubtedly march into the territory of undefined behavior which is what will happen when you try to initialize str with the "string" pointed to by s, since it's not really a string.

14
  • Not to mention that since s isn't even a pointer to a proper C string, at best you will get something meaningless in the std::string, and in the worst case your program will crash when it tries to read from unallocated memory. Feb 27, 2016 at 16:28
  • How does string str = s; possibly create an object unrelated to pointer s, when s was essential to its construction? Do you mean unrelated to it's address?
    – GManNickG
    Feb 27, 2016 at 16:31
  • @GManNickG I didn't say a new pointer, said a new object, and attempting to get the address of the new object will result in a pointer to that new object and that is not related to s in any way. Feb 27, 2016 at 16:36
  • 1
    @GManNickG: Even though the constructor takes in s it does not store the actual value of s in any way within the object. All it does is copy the (nonsensical) data pointed to it into the std::string. Therefore I don't think the resulting std::string has a lasting relationship to s. Feb 27, 2016 at 16:39
  • 1
    @GManNickG But the pointer s is only passed as an argument to the constructor (in this case), that's the closest relationship the string object str and the pointer s have. Once the constructor returns how is str and s related in any way? They aren't. The string object str has its own pointer, that is pointing to some completely other memory than s. Feb 27, 2016 at 16:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.