+ 1
What happens when you cast a pointer to another pointer pointing to a pointer?
I only know pointers to the extent that they hold the memory address of another variable. I also know it is possible to cast a pointer to another pointer pointing to another type: `bool* p = (bool*)&x; // x is an int` where in this case I found out you could dereference `*p` and get a boolean with a higher value than 1 but limited to one byte. Then I tried: `int** p2 = (int**)&x; // x is still an int` and got a segfault when dereferencing twice `**p2`. What is the meaning of the value you get when you dereference `p2` only once? What memory address is it holding?
1 Answer
+ 2
~ swim ~
Will it always end up as a garbage value or are there exceptions such as to get the vtable of a class? I really mostly asked this question because of this snippet:
// FooBar foobar; Foo foo; somewhere up there
long *foobarAsLong = (long *)&foobar;
long *fooAsLong = (long *)&foo;
printf("FooBar vtable pointer: %p\n", foobarAsLong[0]);
printf("Foo vtable pointer: %p\n", fooAsLong[0]);
long **foobarVtable = (long **)&foobar;
long **fooVtable = (long **)&foo;
// This is the address of FooBar::v()
printf("First entry of FooBar VTABLE: %p\n", foobarVtable[0][0]);
// This is the address of Foo::v()
printf("First entry of Foo VTABLE: %p\n", fooVtable[0][0]);
Which I got from https://defuse.ca/exploiting-cpp-vtables.htm