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. Advertising

  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. Advertising

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. bd
    Replies:
    0
    Views:
    634
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,998
    Smokey Grindel
    Dec 2, 2006
  3. Justin Piper
    Replies:
    4
    Views:
    459
    Justin Piper
    Sep 22, 2006
  4. Hakirato
    Replies:
    4
    Views:
    888
    Alf P. Steinbach
    Oct 5, 2006
  5. Carsten Fuchs
    Replies:
    45
    Views:
    1,556
    James Kanze
    Oct 8, 2009
Loading...

Share This Page