Thread: how to check is this void pointer

  1. #1
    Registered User
    Join Date
    Nov 2016
    Posts
    26

    how to check is this void pointer

    Dear All,

    could you please help me on how to check if the poiner is void pointer.



    #
    Code:
    include<iostream>
    using namespace std;
    struct pack
    {
            int a;
            unsigned int b;
    };
    void getno();
    int main()
    { 
            pack *p1=new pack;;
            p1->a=34;
            p1->b=56;
            void *p2=(void *)p1;
                                      // please post here the condition that how do i check if is this void pointer                           
            pack *p3=static_cast<pack*>(p2);
            cout<<p3->a;
            getno();
            return 0;
    }
    void getno()
    {
            cout<<"pack1";
            return;
    }

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    "void" meaning 'contains nothing' would it simply just be a case of testing for this state?

    Code:
    if (ptr == NULL) { // do something }
    EDIT:

    Also you do not generally need 'return' statements in void functions unless you absolutely have to exit the function before the terminating brace.
    Last edited by swgh; 11-08-2016 at 02:50 AM. Reason: Forgot statement
    Double Helix STL

  3. #3
    Registered User
    Join Date
    Nov 2016
    Posts
    26
    Quote Originally Posted by swgh View Post
    "void" meaning 'contains nothing' would it simply just be a case of testing for this state?

    Code:
    if (ptr == NULL) { // do something }
    EDIT:

    Also you do not generally need 'return' statements in void functions unless you absolutely have to exit the function before the terminating brace.
    I agree with you . but what is the difference b/w void and (void *).


    I mean to ask you like in the code i have posted, i would like to check is this void pointer before casting using this line pack *p3=static_cast<pack*>(p2);.
    please see my code which i posted at first

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,347
    Quote Originally Posted by SAP_7
    but what is the difference b/w void and (void *).
    They are different types:
    • "The void type has an empty set of values. The void type is an incomplete type that cannot be completed. It is used as the return type for functions that do not return a value." (from C++11 Clause 3.9.1 Paragraph 9)
    • The void* type is the type "pointer to void". It is used to generically point to objects (which doesn't mean objects of class type only, hence a pointer to int can be converted to a pointer to void), with caveats such as that a pointer of this type cannot be dereferenced.


    Quote Originally Posted by SAP_7
    I mean to ask you like in the code i have posted, i would like to check is this void pointer before casting using this line pack *p3=static_cast<pack*>(p2);.
    please see my code which i posted at first
    Bearing in mind what I wrote above, it is obvious that your question does not make sense: the type of p2 is already known from a visual inspection of the code to be a pointer to void (or "void pointer", if you prefer), since it was declared to be a pointer to void.

    Maybe you are asking how to check if p2 is not a null pointer, but it is certainly not a null pointer because it is initialised from p1. p1 cannot be a null pointer since it is initialised with the result of new pack, which would throw std::bad_alloc on allocation failure (or possibly some other exception, e.g., on constructor failure, though that is not applicable here). If you did want to check if p2 is not a null pointer, using it in a boolean context would do as what swgh demonstrated in post #2, though personally I would rather write:
    Code:
    if (p2)
    {
        // p2 is not a null pointer
    }
    if (!p2)
    {
        // p2 is a null pointer
    }
    Also, note that if you are compiling with respect to C++11 or later, you should use nullptr instead of NULL.

    Note that you do not need the cast here:
    Code:
    void *p2=(void *)p1;
    Any pointer to object type is implicitly convertible to pointer to void, so this would suffice:
    Code:
    void* p2 = p1;
    However, the cast here is required, and it is good to see that you used the most appropriate cast:
    Code:
    pack *p3=static_cast<pack*>(p2);
    Even though the OS is likely to reclaim the memory allocated after the process terminates, it is still good practice to match new with delete.
    Last edited by laserlight; 11-08-2016 at 04:11 AM.
    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

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,714
    You can include <typeinfo> and do this
    Code:
      // please post here the condition that how do i check if is this void pointer                           
      if ( typeid(p2) == typeid(void*) ) {
        cout << "It's a void*" << endl;
      }
    But you know this already from inspecting the code.

    But that by itself doesn't mean that you can be sure that p2 is really pointing to a pack structure. The static_cast and subsequent dereference can still fail, even though your void* check passed.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I would honestly avoid <typeinfo> in C++ - we only need it in rare circumstances. We can use templates in C++ to keep track of type and they are generally much more powerful, and if used correctly, simpler and easier to maintain than using void* - plus they carry 0 runtime overhead.

    If you start messing with void*, chances are you'll lose the type somewhere and you will get weird runtime behavior or crashes. If you use templates, you WILL get a compile error if you make a mistake somewhere. It will always works, or it will never work. But no runtime errors unless you're messing with the types somehow.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need of void pointer
    By tubby123 in forum C Programming
    Replies: 8
    Last Post: 07-12-2011, 01:12 PM
  2. Casting to pointer to pointer to void
    By Sharke in forum C Programming
    Replies: 13
    Last Post: 05-12-2009, 08:40 PM
  3. Replies: 4
    Last Post: 08-27-2007, 11:51 PM
  4. Dereference pointer to void pointer to member
    By phil in forum C Programming
    Replies: 5
    Last Post: 04-20-2005, 11:54 AM
  5. Need help on pointer to VOID
    By dv007 in forum C Programming
    Replies: 19
    Last Post: 07-09-2002, 06:15 PM

Tags for this Thread