-3

I have the following bit of code:

int* anInt = new int(5);
uintptr_t memAddr = (uintptr_t)&anInt;
Log("memAddr is: " + std::to_string(memAddr));
int* anotherInt = (int*)&memAddr;
Log("anInt is: " + std::to_string(*anInt));
Log("anotherInt is: " + std::to_string(*anotherInt));

Now I want anotherInt to point to the same value as anInt, but the current code makes anotherInt point to the value of memAddr. How can I set anotherInt to point to the value of anInt using only memAddr?

ManIkWeet
  • 1,298
  • 1
  • 15
  • 36
  • ................. what?! – Lightness Races in Orbit Nov 21 '14 at 12:44
  • int* anotherInt = (int*)*memAddr; – Ari0nhh Nov 21 '14 at 12:48
  • 1
    Don't use C-style casts. – Bartek Banachewicz Nov 21 '14 at 12:52
  • 1
    Your fourth line is a potential aliasing violation and possibly a constraint violation due to alignment. – Deduplicator Nov 21 '14 at 12:52
  • if I do int* anotherInt = (int*)*memAddr; then I get the error: operand of '*' must be a pointer – ManIkWeet Nov 21 '14 at 12:52
  • @BartekBanachewicz: Why not? They don't make anything worse here, and those are `reinterpret_cast`s anyway. – Deduplicator Nov 21 '14 at 12:53
  • @Deduplicator No? Quite the contrary; reinterpretation of an unsigned type as a signed type (and vice versa) is hidden (and it probably shouldn't be). Explicit cast force you to think about that. – Bartek Banachewicz Nov 21 '14 at 12:54
  • @BartekBanachewicz: There is no reinterpretation of an unsigned as a signed, at least immeadiately. There is casting from one pointer-type to a potentially unrelated one though. Also, how is a C-style cast implicit? – Deduplicator Nov 21 '14 at 12:56
  • @Deduplicator In the same way as *not writing something* is. If you want to do A, and you write `doA`, you are explicit. If you write `do` that does A, B or C, depending on the context, that's implicit. And the fact that the value isn't dereferenced in the example doesn't mean it cannot be; the very fact that it "goes trough" different sign interpretation is dangerous here and should be treated like so. – Bartek Banachewicz Nov 21 '14 at 13:04
  • @BartekBanachewicz: There's a difference between loquaciousness and making things obvious. The different sign-interpretation you speak about is dwarfed as a miniscule part of the aliasing problem, as I emphasized above. – Deduplicator Nov 21 '14 at 13:08
  • Changing it to int* anotherInt = reinterpret_cast(&memAddr); doesn't change the issue, output is still: anInt is: 5 memAddr is: 99542396 anotherInt is: 99542396 – ManIkWeet Nov 21 '14 at 13:09
  • `*anotherInt == memAddr` because you said `anotherInt = &memAddr`. `anInt` is a pointer, which makes `&anInt` (a.k.a. `memAddr`) a pointer to a pointer, so `&memAddr` is a pointer to a pointer to a pointer. – molbdnilo Nov 21 '14 at 13:20

1 Answers1

0

You can type cast a pointer directly to an integer. This will make anotherInt point to the same int as anInt.

uintptr_t memAddr = (uintptr_t)anInt;
...
int* anotherInt = (int*)memAddr;

Alternatively you can have memAddr store the address of the pointer (a pointer to a pointer) and use it as such:

uintptr_t memAddr = (uintptr_t)&anInt;
...
// only works if the variable "anInt" is still alive
int* anotherInt = *(int**)memAddr;
such
  • 583
  • 4
  • 9
  • You are indeed correct, I figured it out with this but decided to go with a different, nicer method of identifiers :) – ManIkWeet Nov 22 '14 at 13:26