Thread: casting from pointer to void

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    99

    casting from pointer to void

    I have a function that produces several messages which are implemented as different structs. The only thing the messages have in common is a member called 'type' to indicate which type of message it is. The function therefore returns an array of pointers to void. Pointers to void because the array needs to containt pointers to different struct types.

    After running the function, I need to dereference the pointers to void. Since I cannot dereference a pointer to void, I need to cast it. But to what type? I read somewhere that I need a switch statement to go through all types and find out what type it is. But then again, how do I know if I have cast the pointer to void to the original type.

    QUESTION: Suppose I have a pointer to void that points to a struct. I do know the collection of possible struct's, but I do not know what struct the pointer exactly refers to. Is there a way to find this out?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by django
    I have a function that produces several messages which are implemented as different structs. The only thing the messages have in common is a member called 'type' to indicate which type of message it is.
    Is that really the only thing these message structs have in common? Do you perhaps perform some operation on them for which the precise details varies with each type, but nonetheless is something in common?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Instead of returning a void pointer, why not return a struct like this;
    Code:
    typedef struct
    {
         TypeDescriptor type;
         void *data;
    } Thing;
    Then just check the type member, and convert data appropriately.

    You could also do the same thing, if you know all you possible types in advance, using a union.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Jul 2011
    Posts
    99
    Depends. There are for example order messages which are all orders (like: I want to buy this and that) but who vary a little with respect to the member fields. They get processed in comparable ways. Why do you ask?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by django
    Depends. There are for example order messages which are all orders (like: I want to buy this and that) but who vary a little with respect to the member fields. They get processed in comparable ways. Why do you ask?
    I ask because the more information you provide, the more likely someone might suggest a design that best fits your problem. I was contemplating between just suggesting a solution along the lines of what grumpy proposed in post #3, or going for something that perhaps is more like polymorphism in C++.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jul 2011
    Posts
    99
    Ok, makes sense.

    What grumpy suggests is quite useful. I would also be interested in how you would approach polymorphism like designs in C.

    I just want to make sure: if you cast something from a pointer to void, there is no way to detect the type of struct?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by django
    I would also be interested in how you would approach polymorphism like designs in C.
    One way is to provide a function pointer member instead of a type (descriptor) member. This function pointer would point to a function that takes say, an object or pointer of the Thing struct as an argument. It would cast the data void pointer appropriately and then operate on the resulting object. This way, you can iterate over the array and just call the correct function with each element of the array.

    Quote Originally Posted by django
    I just want to make sure: if you cast something from a pointer to void, there is no way to detect the type of struct?
    Yes, type information is lost unless you save it elsewhere.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Jul 2011
    Posts
    99
    Thank you all, very helpful again. I know enough to proceed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Casting to pointer to pointer to void
    By Sharke in forum C Programming
    Replies: 13
    Last Post: 05-12-2009, 08:40 PM
  2. Casting a pointer to a function to (void*)
    By genter in forum C++ Programming
    Replies: 12
    Last Post: 01-18-2009, 10:05 PM
  3. type casting void *
    By Alexpo in forum C Programming
    Replies: 5
    Last Post: 06-23-2008, 03:05 AM
  4. casting a void* to a pointer to a member function
    By Sebastiani in forum C++ Programming
    Replies: 13
    Last Post: 10-15-2002, 08:57 AM
  5. casting void*
    By Seron in forum C++ Programming
    Replies: 5
    Last Post: 12-25-2001, 08:02 AM