0

Please consider the following code:

const char* cTitle = "MyTitle";
__int64 i = reinterpret_cast<__int64>(ctitle);

Every time i run this code, i get different value of i. Now i want to write a test to check whether we sent to correct title or not, so i am using the following code to achieve the target but unable to get the title using reinterpret_cast:

char* cOrgValue = reinterpret_cast<char*> (i);

Is it even possible to get the original title value and if yes, then is this is correct way to do so?

EDIT

So let me rephrase the question: How can i get the original value of char* after I cast it into __int64 using reinterpret_cast.

vishal
  • 2,258
  • 1
  • 18
  • 27
  • 2
    `cTitle` is a pointer; you are casting a memory address to `__int64`. I guess you actually wanted to "cast" the content of the string? – M.M Dec 12 '17 at 10:01
  • 2
    Every time you run the location of the array containing the string might be placed at different places. What is the *actual* problem you want to solve? *Why* are you casting a pointer to an (non-standard) `__int64` type? (if you want to print the pointer just cast it to `void*`, or store it in a `std::intptr_t` type variable). – Some programmer dude Dec 12 '17 at 10:01
  • @M.M yes, i want to cast the content of the string. – vishal Dec 12 '17 at 10:02
  • Also be careful with `char* cOrgValue = reinterpret_cast (i)`, remember that string literals in C++ are ***constant***. – Some programmer dude Dec 12 '17 at 10:03
  • Well (even if such a cast were possible) you would be reading 8 bytes from a 7-byte memory allocation , which is undefined. Your "scheme" is only viable if the string is exactly 7 or 8 characters long. You would be better off just copying the string into a char array (preferably, wrapped in a container such as `array`, `string` or `vector`) instead of an `__int64` – M.M Dec 12 '17 at 10:03
  • @Someprogrammerdude i want to store the char* value into an __int64 variable and get it back for comparison, i thought this could be done using reinterpret_cast – vishal Dec 12 '17 at 10:08
  • @vishal you just contradicted yourself. The `char*` value is a memory address. It is not the same as the content of the string. The string content lives in a memory location and the `char *` value is the address of that location. – M.M Dec 12 '17 at 10:12
  • 2
    It can indeed be done, and what you do is close to the "correct" way to do it. It's still not a good idea to do it though. And we still don't know *why* you want to do it. There might be better solutions to the problem you *actually* want to solve with this solution. I suggest you take some time to [read about the XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – Some programmer dude Dec 12 '17 at 10:15
  • 1
    What is your *observable* problem? A good formula is "I want/expect this program to print 123, but it prints 456 instead". – n. m. could be an AI Dec 12 '17 at 10:35
  • @n.m. I was expecting that i would get the original text after using reinterpet_cast on the value of i – vishal Dec 12 '17 at 11:52
  • Suppose I don't know what is reinterpret_cast or value or what are the names of your variables. Do you have a program that behaves the way you don't want it to? What does it do? What do you want it to do? Describe in terms of program input and output, not in terms of its internal state. – n. m. could be an AI Dec 12 '17 at 12:19

1 Answers1

2

Don't use reinterpret_cast. That's my advice. Instead, call std::memcpy:

const char cTitle[] = "MyTitle";
std::uint64_t i64 = 0;
static_assert(sizeof(cTitle) >= sizeof(i64), "wrong sizes");
std::memcpy(&i64, cTitle, sizeof(i64));

You get the original value by an inverted call to memcpy.

Note: I've used a char array instead of a char pointer in order to let sizeof(cTitle) be 8 (the relevant part: the number of chars in the read string, including the final \0). With a const char*, sizeof(cTitle) will be sizeof(void*), which is unspecified.

YSC
  • 38,212
  • 9
  • 96
  • 149
  • That's a litteral answer to your question. But is it really what you want to achieve? XY problem? who knows? – YSC Dec 12 '17 at 10:11
  • If you don't know, then why not close the question as off-topic or unclear? Why write an answer to an open-ended question? – default Dec 12 '17 at 10:13
  • 2
    @Default it's called "trying to be helpful". Maybe this will help someone else even if it doesn't help OP – M.M Dec 12 '17 at 10:13
  • I wanted to know whether this was possible using reinterpret_cast or not, i am open to know the other possible ways for this. – vishal Dec 12 '17 at 10:14
  • your assertion should be negated. Careful with `sizeof`, in the OP example he has a pointer. – bolov Dec 12 '17 at 10:18
  • @M.M well sure.. but I currently find it hard to believe that searching for the question title and expecting this answer would be helpful. Not saying it's a bad advice, just that it doesn't answer the question title or the "convert back" question in the text. To me, this answer shows a way to store char* in uint64, not what is being asked. But I guess that's open to debate (which is why I added the initial comment) – default Dec 12 '17 at 10:18
  • @bolov 1/ I think I have the assertion right: `memcpy` will read `sizeof(64)` bytes from `cTitle`, in order to prevent a read outside of the C-string, its size needs to be at least `sizeof(64)` bytes. – YSC Dec 12 '17 at 10:23
  • @bolov 2/ I'm aware of the array/pointer difference, I'll add a note about it to make it explicit, that's a good idea. – YSC Dec 12 '17 at 10:23
  • @YSC you are correct. I assumed you were copying `sizeof(cTitle)`, which would make more sense to me. – bolov Dec 12 '17 at 10:30
  • @bolov I've developed a phobia about writes outside of bounds. I guess you can tell from my way of thinking. – YSC Dec 12 '17 at 10:32