Typecasting void*

Discussion in 'C++' started by Jack Hopson, Aug 20, 2010.

  1. Jack Hopson

    Jack Hopson Guest

    Hi

    I see a lot of code that explicitily typecasts void* to a specific
    pointer type eg a pointer returned by malloc.

    There is also lots of code that relies on implicit typecasting of void*.

    Which is preferable? Are there situations to choose one way over the
    other?

    Thanks.
     
    Jack Hopson, Aug 20, 2010
    #1
    1. Advertising

  2. Jack Hopson

    Öö Tiib Guest

    On 20 aug, 14:55, Jack Hopson <> wrote:
    > I see a lot of code that explicitily typecasts void* to a specific
    > pointer type eg a pointer returned by malloc.


    This is likely C code you see there. In C++ you very rarely need to
    use void*, when dealing with some C library or API. On such cases do
    not use C-style casts use static_cast.

    > There is also lots of code that relies on implicit typecasting of void*.


    Pointer to any non-cv qualified object implicitly converts to void*.
    It is pointless to use explicit cast to void*. Especially in C code,
    where you do not have C++ casts and so you may accidentally cast away
    constness.

    > Which is preferable? Are there situations to choose one way over the
    > other?


    Since casting to void* and from void* are different operations i do
    not understand that question.
     
    Öö Tiib, Aug 20, 2010
    #2
    1. Advertising

  3. In comp.lang.c Jack Hopson <> wrote:
    > Hi


    > I see a lot of code that explicitily typecasts void* to a specific
    > pointer type eg a pointer returned by malloc.


    Casting the result of malloc() is never needed (unless you're
    programming in C++ and not C). It just keeps the compiler from
    issuing a warning when no declaration of malloc() is in scope
    (which may result in an ugly and hard to find bug).

    > There is also lots of code that relies on implicit typecasting of void*.


    > Which is preferable? Are there situations to choose one way over the
    > other?


    Since a void pointer is automatically converted to the type
    of pointer its assigned to is nearly never needed to cast a
    void pointer and I would avoid adding a cast since to me a
    cast is an indication that something potentially dangerous is
    going on that may require closer scrutiny. The only place a
    cast is necessary is when the compiler can't derive what you
    want by itself or you would have to do arithmetic on a void
    pointer which is not allowed. One possility is that you have
    e.g. a pointer that you know points to an array of ints but
    which the compiler does not know, e.g. because the void poin-
    ter was received as a function argument. If you then need the
    address of the i-th element of the int array then you have to
    cast:

    addr_of_ith_elem = ( int * ) void_ptr + i;

    or

    addr_of_ith_elem = ( char * ) void_ptr + i * sizeof( int );

    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
     
    Jens Thoms Toerring, Aug 20, 2010
    #3
  4. Jack Hopson

    Seebs Guest

    On 2010-08-20, Jack Hopson <> wrote:
    > I see a lot of code that explicitily typecasts void* to a specific
    > pointer type eg a pointer returned by malloc.
    >
    > There is also lots of code that relies on implicit typecasting of void*.
    >
    > Which is preferable? Are there situations to choose one way over the
    > other?


    You posted this to one group where one is clearly superior, and another
    group where the other is clearly superior.

    I think that pretty much summarizes it.

    (Howdy, C++ folks! Let's all stun the onlookers by NOT getting into a
    huge flame war about this, 'k?)

    -s
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
     
    Seebs, Aug 20, 2010
    #4
  5. Jack Hopson wrote:

    > Hi
    >
    > I see a lot of code that explicitily typecasts void* to a specific
    > pointer type eg a pointer returned by malloc.
    >
    > There is also lots of code that relies on implicit typecasting of void*.
    >


    What is "implicit typecasting"? I have always never heard about it (note the
    pun).

    > Which is preferable? Are there situations to choose one way over the
    > other?
    >


    Do you say you want to advertise doing this one?

    void *p = (void*)ptr;

    Please, this is horrible.
     
    Johannes Schaub (litb), Aug 20, 2010
    #5
  6. Jack Hopson

    Guest

    Jack Hopson <> wrote:
    >
    > I see a lot of code that explicitily typecasts void* to a specific
    > pointer type eg a pointer returned by malloc.
    >
    > There is also lots of code that relies on implicit typecasting of void*.
    >
    > Which is preferable? Are there situations to choose one way over the
    > other?


    The answer is different for C and C++. In C, the concensus is that you
    should never use the cast. In C++, the cast is required by the
    language.
    --
    Larry Jones

    The hardest part for us avant-garde post-modern artists is
    deciding whether or not to embrace commercialism. -- Calvin
     
    , Aug 20, 2010
    #6
  7. Jack Hopson <> wrote:
    > I see a lot of code that explicitily typecasts void* to
    > a specific pointer type eg a pointer returned by malloc.


    I think it's safe to say that the majority of C programmers
    who do this are aping other C programmers (even authors of
    C books) who have also merely seen it, but never really
    understood why they do it, let alone why they should or
    shouldn't.

    That isn't to say there aren't C programmers who know what
    they're doing when they cast malloc. Some use C++ compilers
    on C code precisely because of it's stricter requirements,
    such as requiring function prototypes.

    > There is also lots of code that relies on implicit
    > typecasting of void*.
    >
    > Which is preferable?


    The whole point of the implicit conversion of void * in C
    is to write generic code easily. Unfortunately, it's not a
    fashion that has taken off in C. Despite the cleaner syntax,
    it is still difficult to do, and most bugs and problems
    centre around the type weakness that was meant to make
    things simpler.

    It is generally preferable to avoid casts. You generally
    only use casts in C when it is obvious that they are correct.
    Consider...

    printf("%02X", 'A' + 0u);
    printf("%02X", (unsigned) 'A');

    Here %X requires an unsigned int. The cast ensures that, but
    as the first statement shows, it's not strictly necessary.
    This is an example of where most C programmers would actually
    prefer to see the cast, because the implicit conversion in
    the earlier argument is not immediately apparent at a glance.

    Of course, it can be also done as...

    unsigned x = 'A';
    printf("%02X", x);

    But that comes at the cost of declaring a variable. Apart
    from the aesthetics, every line of code is an opportunity
    for something (else) to go wrong. ;)

    Everything is a trade-off. Casts are no exception.

    --
    Peter
     
    Peter Nilsson, Aug 21, 2010
    #7
  8. Jack Hopson

    Nick Guest

    writes:

    > Jack Hopson <> wrote:
    >>
    >> I see a lot of code that explicitily typecasts void* to a specific
    >> pointer type eg a pointer returned by malloc.
    >>
    >> There is also lots of code that relies on implicit typecasting of void*.
    >>
    >> Which is preferable? Are there situations to choose one way over the
    >> other?

    >
    > The answer is different for C and C++. In C, the concensus is that you
    > should never use the cast. In C++, the cast is required by the
    > language.


    About the only time in C when you might want to is in a function that
    takes a generic argument (often a callback function). But even then,
    it's always clearer to do:
    int callback(void *data) {
    struct data_block *db = data;
    printf("%s\n",db->name);
    }

    than:
    int callback(void *data) {
    printf("%s\n",((struct data_block *)data)->name);
    }

    even if (because?!) the second is one line shorter and doesn't use the
    extra variable.
    --
    Online waterways route planner | http://canalplan.eu
    Plan trips, see photos, check facilities | http://canalplan.org.uk
     
    Nick, Aug 21, 2010
    #8
    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. Ollej Reemt
    Replies:
    7
    Views:
    568
    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:
    812
    The Real OS/2 Guy
    Oct 28, 2003
  3. Replies:
    3
    Views:
    602
    Emmanuel Delahaye
    Jul 15, 2005
  4. Replies:
    5
    Views:
    850
    S.Tobias
    Jul 22, 2005
  5. Replies:
    1
    Views:
    419
    Victor Bazarov
    May 23, 2007
Loading...

Share This Page