Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
C0D4669447y@R1100 try looking up void types on variables, Or better yet give *gen a usable type 😟
@localjoost you bet me to it 😂 -
ynnuW967yYou need to cast gen to a short pointer,
res = *(short *)gen + 7
The code makes no sense at all though. -
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.
Hi !
Can you tell me what is wrong with this c++ code?
Thanks alot !
question