10
R-C-D
7y

Hi !
Can you tell me what is wrong with this c++ code?
Thanks alot !

Comments
  • 1
    Lol. Not sure if joking?
  • 4
    I may not know c++ but god damn read the error log 🤦‍♂️
  • 0
    @C0D4 I read them !
    I just dont know how to solve it or why it has occured
  • 6
  • 0
    @R1100 try looking up void types on variables, Or better yet give *gen a usable type 😟

    @localjoost you bet me to it 😂
  • 0
    Can you explain why gen is of type void?
  • 0
    @Olverine it is just a practice about pointers I found somewhere...
  • 4
    You need to cast gen to a short pointer,

    res = *(short *)gen + 7

    The code makes no sense at all though.
  • 5
    I think your own post fits quite well here 😁
    No intend to offend you btw :3 😊
  • 1
    @DataSec 😂😂 right 👍
  • 0
    @ynnuW worked ! Thanks !
  • 0
    Num is not initialized
  • 1
    1. The num pointer isn't pointing to anything, it's just an uninitialized pointer variable. Give it a different variable's address to point to, or use "new" to allocate a chunk of memory for it.

    2. I do believe you need to cast the short pointer to a void pointer, but then I'm not sure. Casting should be done anyway, letting the compiler silently coerce types is generally a terrible idea. Read up on static_cast, dynamic_cast, and reinterpret_cast, C++'s cast operators.

    3. You're trying to dereference a void pointer, which can't be done because void pointers don't carry associated type information, they're meant to be treated as generic memory addresses. Plus, said void pointer (gen) has the contents of num, which is to say, a garbage address.

    Given these mistakes, I would say you don't understand pointers and memory management in C/C++ at all. Read up on the basics before doing gymnastics with void pointers, a good C or C++ book would be a great way to start.
  • 0
    @RememberMe thanks ! Now I'm clear !!
Add Comment