Like Tree1Likes
  • 1 Post By Codeplug

Is this standard-conforming?

This is a discussion on Is this standard-conforming? within the C++ Programming forums, part of the General Programming Boards category; I'm trying to find this in C++ standard (ISO/IEC 14882) but unfortunately I can't. Code: // external functions (in DLL ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    681

    Is this standard-conforming?

    I'm trying to find this in C++ standard (ISO/IEC 14882) but unfortunately I can't.
    Code:
    // external functions (in DLL if this matters at all)
    extern "C" int Set(void* Ptr);
    extern "C" int Get(void** Ptr);
    
    // ...
    
    UserClass* a;
    Set(a);
    
    // ...
    
    UserClass* ptr;
    Get(reinterpret_cast<void**>(&ptr));
    Is this standard conforming? Will it result in undefined behaviour? I am SURE that Get() will return pointer to UserClass not to something else.

    I have my doubts because there is a hidden conversion from UserClass* to void* (I know I can solve this using a temporary variable).
    Last edited by kmdv; 03-17-2011 at 09:46 AM.
    I never put signature, but I decided to make an exception.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,652
    >> Is this standard conforming?
    Yes. Any pointer type can be safely cast to a void* and then back again to the original pointer type. The only exception is that member [function] pointers do not have a guaranteed round-trip though a void*.

    gg

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,211
    If a is uninitialised, then accessing its value in the call "Set(a)" gives undefined behaviour.

    Beyond that, after the call to Get(), ptr is not guaranteed to contain the value of a (the pointer provided to Get()) unless you judiciously use reinterpret_cast inside the Get() function. Which is unlikely to be happening, if Get() is written in C and not written with knowledge of what UserClass is.

    You would be better off having Get() of the form
    Code:
    extern "C" void *Get();
    A simple static_cast<> on the return value of that will retrieve the address of the original object.
    Right 98% of the time, and don't care about the other 3%.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    681
    Thanks for the replies.

    I forgot to initialize, but this is only a sample, illustrating the reinterpret_cast<> problem. I know that any pointer can be casted to void*, but the problem is, this cast is "hidden" here. In fact, this conversion does not take place at all (I was wondering if pointer representation is guaranteed to be the same for all types)

    I know that I can return this pointer, but the return variable is reserved for error codes.

    Quote Originally Posted by Codeplug View Post
    The only exception is that member [function] pointers do not have a guaranteed round-trip though a void*.
    Good to know this.
    I never put signature, but I decided to make an exception.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,211
    Quote Originally Posted by kmdv View Post
    I know that any pointer can be casted to void*, but the problem is, this cast is "hidden" here.
    Your problem is with having two levels of indirection, not one (i.e. converting a pointer to pointer into a pointer to pointer to void). That's why you need the reinterpret cast.

    A pointer to pointer to X is not implicitly convertible to a pointer to pointer to void for a reason.

    Quote Originally Posted by kmdv View Post
    In fact, this conversion does not take place at all (I was wondering if pointer representation is guaranteed to be the same for all types)
    I assume you mean the conversion is not changing value for your compiler.

    The standard guarantees that conversion of a pointer into a void pointer, and back, yields the original pointer. That is not quite the same as saying that all pointers (other than to member functions) must have the same representation although, obviously, that is a common way to achieve it.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Automatic flush of standard streams in stdio
    By Aculaniveus in forum C Programming
    Replies: 9
    Last Post: 01-24-2010, 12:30 PM
  2. Standard efficiency
    By Jorl17 in forum C Programming
    Replies: 3
    Last Post: 06-18-2009, 11:48 AM
  3. array initialization & C standard
    By jim mcnamara in forum C Programming
    Replies: 2
    Last Post: 09-12-2008, 03:25 PM
  4. C/C++ standard
    By confuted in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 07-06-2003, 03:22 AM
  5. standard language, standard compiler?
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 09-03-2001, 04:21 AM

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