Re: float** to void** - why does it require an explicit cast?

Discussion in 'C++' started by Alf P. Steinbach /Usenet, Jul 26, 2010.

  1. * Stuart Golodetz, on 26.07.2010 21:07:
    > Hi guys,
    >
    > Just wondering what the reason is for not being able to implicitly
    > convert from (say) a float** to a void** please? (It's probably
    > something I ought to know, but I don't...) I saw some code earlier which
    > looked a bit like:
    >
    > float *p;
    > cudaMalloc((void**)&p, nsize * sizeof(float));
    >
    > (where the idea is that cudaMalloc sets p to point to the allocated
    > memory), and I couldn't remember at the time if, and why, the cast was
    > necessary. I've subsequently tried it on several compilers, and it is
    > indeed necessary (it's a reinterpret_cast using the C++-style casts) --
    > but I'm still not sure as to the why. Please could someone enlighten me?
    > :) Is it a safety issue (like the issue with converting T** to const T**
    > discussed in the FAQ),


    Yes, exactly.


    > or something else?


    No. :)

    float f = 3.14;
    char c[] = "Hubba hubba";

    float* pf = &pf;
    float** ppf = &pf;

    void** ppv = ppf; // Pretend that this is supported.
    void*& pv = *ppv;
    pv = &c[0]; // Assigning to pf


    Cheers & hth.,

    - Alf

    --
    blog at <url: http://alfps.wordpress.com>
     
    Alf P. Steinbach /Usenet, Jul 26, 2010
    #1
    1. Advertisements

  2. Alf P. Steinbach /Usenet

    James Kanze Guest

    On Jul 26, 8:39 pm, "Alf P. Steinbach /Usenet" <alf.p.steinbach
    > wrote:
    > * Stuart Golodetz, on 26.07.2010 21:07:
    > > Just wondering what the reason is for not being able to
    > > implicitly convert from (say) a float** to a void** please?
    > > (It's probably something I ought to know, but I don't...)
    > > I saw some code earlier which looked a bit like:


    > > float *p;
    > > cudaMalloc((void**)&p, nsize * sizeof(float));


    > > (where the idea is that cudaMalloc sets p to point to the
    > > allocated memory), and I couldn't remember at the time if,
    > > and why, the cast was necessary. I've subsequently tried it
    > > on several compilers, and it is indeed necessary (it's
    > > a reinterpret_cast using the C++-style casts) -- but I'm
    > > still not sure as to the why. Please could someone enlighten
    > > me? :) Is it a safety issue (like the issue with converting
    > > T** to const T** discussed in the FAQ),


    > Yes, exactly.


    It's even worse than that. On many older machines (and perhaps
    some of the more exotic modern ones---maybe some embedded
    processors?), a void* didn't have the same size and
    representation as a float*. In this case, this sort of type
    punning comes into the same category of something like:
    int i;
    std::cout << *(float*)(&i);
    Posix (and I suspect Windows as well) requires all pointers
    (data and function) to have the same size and format, and this
    is true for IBM mainframes as well. So the code will work on
    most of the machines that most programmers are likely to
    encounter.

    --
    James Kanze
     
    James Kanze, Jul 27, 2010
    #2
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ollej Reemt
    Replies:
    7
    Views:
    888
    Jack Klein
    Apr 22, 2005
  2. Stig Brautaset

    `void **' revisited: void *pop(void **root)

    Stig Brautaset, Oct 25, 2003, in forum: C Programming
    Replies:
    15
    Views:
    1,040
    The Real OS/2 Guy
    Oct 28, 2003
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,823
    Smokey Grindel
    Dec 2, 2006
  4. Replies:
    5
    Views:
    1,137
    S.Tobias
    Jul 22, 2005
  5. Justin Piper
    Replies:
    4
    Views:
    557
    Justin Piper
    Sep 22, 2006
  6. Hakirato
    Replies:
    4
    Views:
    1,156
    Alf P. Steinbach
    Oct 5, 2006
  7. regis

    void * and explicit cast

    regis, Dec 1, 2008, in forum: C Programming
    Replies:
    22
    Views:
    1,320
    Tim Rentsch
    Jan 7, 2009
  8. Carsten Fuchs
    Replies:
    45
    Views:
    2,263
    James Kanze
    Oct 8, 2009
Loading...