-1

Studying C++ with some sources I often came to see this, I'm a bit confused about this part of example source code (game server, on packet delete part) :

auto opcode = **reinterpret_cast<uint16_t**>((static_cast<char*>(packet) + 0x1050));
auto ptr = (char*)(*(void**)(static_cast<char*>(packet) + 0x1034));
uint16_t raw = *(const uint16_t*)ptr, packetSize = raw & 0x7FFF;

Image (marked as red squares) :

Image

  • What do the pointers in front of parentheses means ? (*(void**), *(const uint16_t*)

  • What do the double pointers in front of casting functions ? **reinterpret_cast<uint16_t**>

milleniumbug
  • 15,379
  • 3
  • 47
  • 71
Dentrax
  • 574
  • 8
  • 22
  • 2
    Do yourself a favor and abandon all those stars. And casts. – Ron Aug 21 '17 at 20:28
  • Haha :) I'm not a skilled C++ developer. I have just wondered. I did research on Google but I did not anythink find abaut it. :) – Dentrax Aug 21 '17 at 20:31
  • 4
    It's dereferencing a pointer. But this code is a mish-mash of styles (and looks really suspicious) – UnholySheep Aug 21 '17 at 20:31
  • Don't go astray in an unreadable, suspicious and dangerous labyrinths. – Raindrop7 Aug 21 '17 at 20:37
  • 4
    Since you're a beginner, don't dig into what this code means. It's horrible stuff. It will only give you a headache. – Pete Becker Aug 21 '17 at 20:44
  • 2
    dude where did you find this stuff. At first I thought it was some machine generated code but that last line just looks like intentionally misleading code. Why would someone use the comma operator where a semicolon would do the same. – PeterT Aug 21 '17 at 20:51
  • 1
    Here is a list of some [fine C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Ron Aug 21 '17 at 21:11
  • Thank you for the comments. But I do not understand why question got -rep points. :) – Dentrax Aug 21 '17 at 22:22

2 Answers2

4

It is not pointer - it is operator dereferencing pointer.

you can always split this expression into smaller parts to analyze it.

x = **reinterpret_cast<uint16_t**>(something)

could be read as

temp1 = reinterpret_cast<uint16_t**>(something); //temp1 is uint16_t**
temp2 = *temp1; // dereference pointer temp1 - got uint16_t*
x = *temp2; //well x is uint16_t to which temp2 was pointing to...

you can do the same for all other stars you have marked

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Artemy Vysotsky
  • 2,694
  • 11
  • 20
1

The asterix * mean dereferencing. Dereferencing mean, use the pointer as an address and get the content a this address. How the content will be interpreted depends on further parts of the type info

After applying reinterpret_cast<uint16_t**> you have a pointer to a pointer of uint16_t. When dereferencing twice you get an uint16_t.

*(void**): this means, you interpret the right, not mentioned side as a pointer to a pointer of void. By dereferencing once, you got a pointer of void.

*(const uint16_t*): this means, you interpret the right, not mentioned side as an pointer a const uint16_t. By dereferencing once, you got that const uint16_t.

stefan bachert
  • 9,413
  • 4
  • 33
  • 40