dereferencing a void *

This is a discussion on dereferencing a void * within the C++ Programming forums, part of the General Programming Boards category; i use a void pointer to point either one of two distinct class types. say: class x{...} class y{...} and ...

  1. #1
    pointing
    Guest

    Question dereferencing a void *

    i use a void pointer to point either one of two distinct class types.

    say:

    class x{...}
    class y{...}

    and

    void *p;
    x object1;
    y object2;

    p = (x *)&object1;


    etc....
    and somehow when i need to reach to one of object1's fields:
    i want to use p to reach the object but
    i receive

    "attempt to dereference a generic pointer" error?
    is there anybody who has faced this error before?
    thanks in advance

  2. #2
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    You have to typecast the void pointer to a pointer to the type you want to access.

    so

    *(x*)(p)

    But you really should not have to deal with void pointers much in C++ because of templating and inheritance. void pointers are generally more necissary in C than C++.

  3. #3
    Registered User pointing's Avatar
    Join Date
    Dec 2002
    Posts
    4
    yeah that was pretty much the same solution that i have tried
    but it didn't work
    you might be right about not using void * in C++

    but does anyone has an advice to use what when i need to point two distict types via only one pointer.

    (i'm implemeting a b+ tree and i need to point to internal nodes and leaf nodes with a unique type of pointer)

  4. #4
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    That should work. Make sure you encase that in parenthesis before you use it. You can use the arrow operator instead of dereferencing the cast pointer as well.

  5. #5
    Registered User pointing's Avatar
    Join Date
    Dec 2002
    Posts
    4
    that's what i'm doing actually,
    it's probably a property of C++ but i couldn't fix
    let me be more informative:

    class Int_Node{
    public:
    //internal node
    vector<int> keys;
    vector<void *> pointers;
    };

    class Leaf_Node{
    public:
    //leaf node
    vector<int> records;
    Int_Node *parent_node;
    };

    Leaf_Node bk2;
    //......bk2 leaf has an internal node as the parent node
    //then i want to reach the sibling leaf node

    cout << ((Leaf_Node *)(bk2.parent_node->pointers[1]))->records[0];

    //and this line of code gives the error
    //i hope this code clearifies the problem

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I can think of several solutions to your problem. What you are trying to do would be the first thing I would so as it is the easiest.

    Example
    Code:
    int *i = new int;
    char j[4] = "hi!!";
    
    i = (int *)j; //casting a char array into an int pointer
    
    //or for an object
    
    struct a {
        int i;
    };
    
    struct b {
        char j[4];
        b() {
             std::memcpy(j, "hi!!", 4);
        }
    };
    
    a A;
    b *B = (b *)&A;
    
    ((a *)b)->i = 6;
    Get it? You may also want to make an abstract base class.

  7. #7
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072

    Re: dereferencing a void *

    Originally posted by pointing
    i use a void pointer to point either one of two distinct class types.
    Perhaps an abstract base class is a possible solution?
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  8. #8
    Registered User
    Join Date
    Dec 2002
    Posts
    119
    Quoting master5001
    >int *i = new int;

    Remember to delete...

    delete i;

    >char j[4] = "hi!!";

    Needs to be five elements long for the terminating null

    char* j = "hi!!";
    char j[] = "hi!!";
    char j[5] = "hi!!";

  9. #9
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by Codulation
    char* j = "hi!!";
    Just watch out if you do that -- it's not the same as the other two. It creates a pointer to the first element of a string constant in the text area of your program while the other two examples you gave actually make an array with that data. In this case the data isn't being modified, but if you ever want to change the value of a letter then you'd most-likely get an error because you'd be trying to access a section of the programs memory that should not be being altered at runtime!

  10. #10
    Registered User
    Join Date
    Dec 2002
    Posts
    119
    Thanks for clarifying that PolyOOP, That syntax can be used like a char array, but attempted modifications will have undefined results (most likely a seg fault).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to better manage large .cpp files
    By 39ster in forum C++ Programming
    Replies: 6
    Last Post: 08-25-2008, 09:24 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. ChangeDisplaySettings - Blank?
    By Tonto in forum Windows Programming
    Replies: 13
    Last Post: 12-26-2006, 04:17 PM
  4. Pls repair my basketball program
    By death_messiah12 in forum C++ Programming
    Replies: 10
    Last Post: 12-11-2006, 05:15 AM
  5. msvc just ate one of my source files
    By Eber Kain in forum C++ Programming
    Replies: 6
    Last Post: 07-01-2004, 06:40 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21