Thread: What is void* ?

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    186

    What is void* ?

    I have to pass a parameter of type void *arg. Does that just mean it can be a pointer to anything?

    I'm trying to send (void*) *it where *it dereferences a pointer to my object that I want to pass. It says I made an invalid cast. I tried (void*) it, but that didn't work either.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, void * is "a pointer to something, not sure what" type of thing, and it is most often used to have a generic pointer to "anything".

    It is more a C than a C++ thin, as in C++ there are often ways to store pointers to a limited range of "anythings" using better methods. In C, there are no class hierarchies or template functionality to allow you to deal with "this could be just about anything".

    It is still used occasionally in C++ too, but it's best avoided if there is a different solution.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If you are receiving a void* and you are planning to dereference it, you have to know what type the pointer actually points to and cast it back to a correct pointer type.

    That's hard to get right since the compiler will just accept anything you do, and that's why they are advised against in C++.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    Well the thing is, I'm calling the method and I need to provide "void *arg" as a parameter to the function, so I'm trying to send an object but the compiler says I'm doing an invalid cast.

  5. #5
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    Showing some code might help...

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by jcafaro10 View Post
    Well the thing is, I'm calling the method and I need to provide "void *arg" as a parameter to the function, so I'm trying to send an object but the compiler says I'm doing an invalid cast.
    In C++, you'll need to use:

    Code:
    reinterpret_cast<void *>(whateverPointer)
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #7
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    Just out of curioisity, what does that mean?

  8. #8
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    Here's some sample code:
    Code:
    for(list<Requester>::iterator it = myList.begin() ; it != myList.end(); ++it)
    {
          some_function((void *) *it);
    }
    Code:
    some_function(void *arg)//I'm given this
    {
         //Code I'm given
    }

  9. #9
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    This won't compile:

    Code:
    	for(list<Requester>::iterator it = activeRequesters.begin();it!=activeRequesters.end();++it)
    	{
    		some_function(reinterpret_cast<void *>(it)));
    	}

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What does this function expect the void * to be? At the very least it needs to be a pointer, which *it is not. Do you mean &*it?

  11. #11
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    Wow, &*it compiled. I'm not sure what that means. If it is a pointer and * dereferences it then isn't it redundant to say & because doesn't that just give the address, which is what the pointer is in the first place?

    I suppose I don't really understand what exactly is happening here.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by jcafaro10
    Wow, &*it compiled. I'm not sure what that means. If it is a pointer and * dereferences it then isn't it redundant to say & because doesn't that just give the address, which is what the pointer is in the first place?
    Ah, but it is not just a pointer that is involved here. it is an iterator. *it is the object that the iterator points to. &*it is the address of the object that the iterator points to.

    Note that conversion to void* is perfectly safe (though it loses type information), so an explicit cast is unnecessary.
    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

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by brewbuck View Post
    In C++, you'll need to use:

    Code:
    reinterpret_cast<void *>(whateverPointer)
    No you don't. All object pointers can be implicitly converted to void*. If the source is const, the reinterpret_cast cannot cast it away. And if the source is a function pointer, the cast is completely unsupported, and you shouldn't do it anyway.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by jcafaro10 View Post
    Wow, &*it compiled. I'm not sure what that means. If it is a pointer and * dereferences it then isn't it redundant to say & because doesn't that just give the address, which is what the pointer is in the first place?

    I suppose I don't really understand what exactly is happening here.
    it, in this case, is an iterator. It's a class that emulates a pointer.
    Iterator overload the operator *, so you can "dereference" them to get their value.
    When the iterator then returns its value, you can take the address of that value with &.
    It's a common trick in C++. Naturally, if it was not an iterator, then it would indeed be redundant.
    Do note that void* is a pointer, and as thus you must pass an address and not a value.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by CornedBee View Post
    No you don't. All object pointers can be implicitly converted to void*. If the source is const, the reinterpret_cast cannot cast it away. And if the source is a function pointer, the cast is completely unsupported, and you shouldn't do it anyway.
    You're right, I was thinking of the other direction. In C you can convert to/from void * from any pointer type without a cast. In C++ you have to cast when converting void * back to something else, but not converting TO void *.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed