Casting to pointer to pointer to void

This is a discussion on Casting to pointer to pointer to void within the C Programming forums, part of the General Programming Boards category; The following snippet from a DirectX tutorial I'm reading: Code: VOID* pVoid; v_buffer->Lock(0, 0, (void**)&pVoid, 0); I'm a little confused ...

  1. #1
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303

    Casting to pointer to pointer to void

    The following snippet from a DirectX tutorial I'm reading:

    Code:
    VOID* pVoid;  
    
    v_buffer->Lock(0, 0, (void**)&pVoid, 0);
    I'm a little confused about the (void**) cast. Isn't &pVoid a void** anyway?
    Last edited by Sharke; 05-10-2009 at 12:06 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That depends, what did you really do with VOID? VOID isn't a keyword, void is.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303
    Quote Originally Posted by quzah View Post
    That depends, what did you really do with VOID? VOID isn't a keyword, void is.

    Quzah.
    I presumed it was a DirectX type. Here's the specification for lock():

    Code:
    HRESULT Lock(UINT OffsetToLock,
                 UINT SizeToLock,
                 VOID** ppbData,
                 DWORD Flags);

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    1. VOID is Microsoft Windows API type
    2. &pVoid has type VOID** - which is exactly what the function expects - so no cast is needed
    3. cast to void** is strongly speaking - wrong thing to do because it is not guaranteed that VOID will always be a synonim to void
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    I agree with vart. It looks like a potential bug in the tutorial.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303
    Quote Originally Posted by laserlight View Post
    I agree with vart. It looks like a potential bug in the tutorial.
    It certainly seems logical to think so. However, I was just looking through a book by Jonathan Harbour today and when he shows how to lock the vertex buffer in DirectX he does exactly the same thing - casts that argument to void**. I've looked around and seen other tutorials which also cast to void** the same way.

    Here's a page of a book by Alan Thorn which also does the cast:

    DirectX 9 Graphics: The Definitive ... - Google Book Search

  7. #7
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    I think everyone's response is trying to get at that because VOID is probably a #define or typedef, and not a C/C++ primitive, you can't guarantee that it's compatible with void.
    **VOID may not be **void
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  8. #8
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303
    Quote Originally Posted by neandrake View Post
    I think everyone's response is trying to get at that because VOID is probably a #define or typedef, and not a C/C++ primitive, you can't guarantee that it's compatible with void.
    **VOID may not be **void
    But if the specification for a function states that it requires a VOID** , then isn't the required guarantee that the argument is VOID**? Thus if, in this case, the actual argument is &pVoid and pVoid has already been defined as a VOID*, then I don't see why it needs a cast to guarantee that it's void**, which isn't the required type stated in the specification anyway. Or maybe I'm just missing something here.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Quote Originally Posted by Sharke
    Thus if, in this case, the actual argument is &pVoid and pVoid has already been defined as a VOID*, then I don't see why it needs a cast to guarantee that it's void**, which isn't the required type stated in the specification anyway.
    That's right.

    However, it is also possible that you could be looking at a change in programming interface, e.g., the tutorial and some books were written for a time when Lock's third parameter was a void**, but later it was changed to a VOID**.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303
    Quote Originally Posted by laserlight View Post
    That's right.

    However, it is also possible that you could be looking at a change in programming interface, e.g., the tutorial and some books were written for a time when Lock's third parameter was a void**, but later it was changed to a VOID**.
    Maybe - but since most of these tutorials also clearly state the DirectX specification which says VOID**, I'm thinking that it's possibly the case that they contain code which has been copied and pasted again and again over the years, or the authors have just come to learn it parrot fashion and have never thought about updating.

  11. #11
    Registered User
    Join Date
    Apr 2009
    Location
    Russia
    Posts
    116
    Does anybody know what difference between void * and void ** and void *** ?
    Where is it can be useful ?
    Who can get an address from void ** ?

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Quote Originally Posted by c.user
    Does anybody know what difference between void * and void ** and void *** ?
    Yes. They are pointer types with different levels of indirection.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Apr 2009
    Location
    Russia
    Posts
    116
    Quote Originally Posted by laserlight
    Yes. They are pointer types with different levels of indirection.
    Code:
        void *p, **pp;
        int n;
    
        p = &n;
        pp = &p;
    if we would write pp = &n; p = &pp what we will get then ?
    I mean, does anything change in the program ?
    Or, we only will get an error for pp = &n and nothing more will change ?

  14. #14
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,246
    Quote Originally Posted by vart View Post
    it is not guaranteed that VOID will always be a synonim to void
    While technically true, any API that defines a symbol VOID which means something other than "void" is just being purposefully confusing and should not be used. Probably, this is a holdover from days when not all compilers supported the void type, and the only real alternative would be char.

    Similarly, if you see some constant FALSE in a piece of code, you technically should not assume that this means 0, but if it doesn't, you should run, not walk, as far from that codebase as possible.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  2. LNK2001 ERROR!!! need help
    By lifeafterdeath in forum C++ Programming
    Replies: 7
    Last Post: 05-27-2008, 05:05 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. linked list problem
    By kzar in forum C Programming
    Replies: 8
    Last Post: 02-05-2005, 03:16 PM
  5. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-01-2003, 11:26 PM

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