casts

Discussion in 'C Programming' started by Bill Cunningham, Jul 30, 2009.

  1. So many people I have noticed in their code use casts. Something
    Richard Heathfield said once very much sticks out in my mind. If code is
    written properly there is no need (or rarely) for casts. Why do they seem to
    be used so much then?

    Bill
     
    Bill Cunningham, Jul 30, 2009
    #1
    1. Advertising

  2. Bill Cunningham

    Mark Bluemel Guest

    On 30 July, 15:37, "Bill Cunningham" <> wrote:
    >     So many people I have noticed in their code use casts.


    Which people where?

    > Something
    > Richard Heathfield said once very much sticks out in my mind. If code is
    > written properly there is no need (or rarely) for casts. Why do they seem to
    > be used so much then?


    Perhaps you're looking at badly written code? Or at least code which
    is not written as well as it might be...
     
    Mark Bluemel, Jul 30, 2009
    #2
    1. Advertising

  3. "Mark Bluemel" <> wrote in message
    news:...
    On 30 July, 15:37, "Bill Cunningham" <> wrote:
    > So many people I have noticed in their code use casts.


    Which people where?

    Perhaps you're looking at badly written code? Or at least code which
    is not written as well as it might be...

    I meant no one in particular. Just lurking clc I see so much like
    (void*) and such but here is one example.

    By Arnuld on clc

    " At my job, I am working on a piece of code (around 16 source files)
    where I have to call malloc() and free() around 20 times. Every time I
    have to check the return value of malloc(). So I cam up with this idea
    of writing my own malloc() and then using it. is this a good idea ?


    #include <stdio.h>
    #include <stdlib.h>


    enum { ARRSIZE = 10 };

    struct my_struct
    {
    int i;
    char arrc[ARRSIZE];
    };


    void* malloc_my_struct(int num, size_t s);
    void free_my_struct( struct my_struct** p );


    int main(void)
    {
    struct my_struct* m = malloc_my_struct(1, sizeof(*m));

    printf("Memory allocated: %p\n", (void*)m);
    free_my_struct(&m);
    printf("Memory freed: %p\n", (void*)m);
    return 0;
    }


    void* malloc_my_struct(int num, size_t s)
    {
    void* p = malloc(num * s);

    if(NULL == p) fprintf(stderr, "IN: %s: Memory Exhausted, can not
    allocate more memory\n", __func__);

    return p;
    }


    void free_my_struct( struct my_struct** p )
    {
    free(*p);
    *p = NULL;
    } "


    ============ OUTPUT ======================
    [arnuld@dune programs]$ gcc -std=c99 -pedantic -Wall -Wextra xmalloc.c
    [arnuld@dune programs]$ ./a.out
    Memory allocated: 0x804a008
    Memory freed: (nil)
    [arnuld@dune programs]$



    I got this idea actually from GNU C Library manual:
    http://www.gnu.org/software/hello/manual/libc/Malloc-Examples.html#Malloc-Examples
    .. 2md, I am using -std=c99 rather than -ansi flag because I am unable
    to find any alternative to __func__ . Do you know any ?
     
    Bill Cunningham, Jul 30, 2009
    #3
  4. On 30 juil, 16:49, "Bill Cunningham" <> wrote:
    Just lurking clc I see so much like
    > (void*) and such but here is one example.
    >   printf("Memory allocated: %p\n", (void*)m);


    Well, C99 says : "The argument shall be a pointer to void. The value
    of the pointer is
    converted to a sequence of printing characters, in an implementation-
    deï¬ned
    manner."
    so in that case, the cast is required.
     
    nicolas.sitbon, Jul 30, 2009
    #4
  5. On 30 Jul 2009 at 14:37, Bill Cunningham wrote:
    > Something Richard Heathfield said once very much sticks out in my
    > mind. If code is written properly there is no need (or rarely) for
    > casts.


    As with most things Heathfield says, this needs to be taken with a pinch
    of salt. As is typical for Heathfield, this statement is pure polemic to
    push his "Sola ISO C" fundamentalism.

    There are many places where casts are essential: for example, to
    implement polymorphism in object-oriented C programming or for type
    punning. There are also many places where casts are useful: for example,
    casting a uint64_t to a uint32_t is a simple way to reduce it mod 2^32.
     
    Antoninus Twink, Jul 30, 2009
    #5
  6. Bill Cunningham

    Fred Guest

    On Jul 30, 10:58 am, Antoninus Twink <> wrote:
    > On 30 Jul 2009 at 14:37, Bill Cunningham wrote:
    >
    > > Something Richard Heathfield said once very much sticks out in my
    > > mind. If code is written properly there is no need (or rarely) for
    > > casts.

    >
    > As with most things Heathfield says, this needs to be taken with a pinch
    > of salt. As is typical for Heathfield, this statement is pure polemic to
    > push his "Sola ISO C" fundamentalism.
    >
    > There are many places where casts are essential: for example, to
    > implement polymorphism in object-oriented C programming or for type
    > punning. There are also many places where casts are useful: for example,
    > casting a uint64_t to a uint32_t is a simple way to reduce it mod 2^32.


    Also, it is difficult to use qsort without using cast.
    --
    Fred K.
     
    Fred, Jul 30, 2009
    #6
  7. Bill Cunningham

    Ben Pfaff Guest

    Fred <> writes:

    > Also, it is difficult to use qsort without using cast.


    I use qsort without casts regularly.
    --
    "It wouldn't be a new C standard if it didn't give a
    new meaning to the word `static'."
    --Peter Seebach on C99
     
    Ben Pfaff, Jul 30, 2009
    #7
  8. Bill Cunningham

    Paul N Guest

    On 30 July, 15:37, "Bill Cunningham" <> wrote:
    >     So many people I have noticed in their code use casts.  Something
    > Richard Heathfield said once very much sticks out in my mind. If code is
    > written properly there is no need (or rarely) for casts. Why do they seem to
    > be used so much then?


    The program I'm writing (which is in C++ rather than C, but similar
    considerations apply) is for Windows. In Windows, there is a function
    called SendMessage which is used to inform a window that various
    things have happened (or occasionally to ask the window something).
    Different messages take different conceptual arguments of different
    types, but these arguments have to be passed to SendMessage in a fixed
    form (there is only one prototype for SendMessage) which means a
    certain amount of casting to get the arguments into the standard form.
    This is where most of the casts in my program seem to stem from.

    Hope that helps.
    Paul.
     
    Paul N, Jul 30, 2009
    #8
  9. Bill Cunningham

    Fred Guest

    On Jul 30, 11:45 am, Richard Heathfield <> wrote:
    > Fred said:
    >
    >
    >
    >
    >
    > > On Jul 30, 10:58 am, Antoninus Twink <> wrote:
    > >> On 30 Jul 2009 at 14:37, Bill Cunningham wrote:

    >
    > >> > Something Richard Heathfield said once very much sticks out in my
    > >> > mind. If code is written properly there is no need (or rarely)
    > >> > for casts.

    >
    > >> As with most things Heathfield says, this needs to be taken with a
    > >> pinch of salt. As is typical for Heathfield, this statement is pure
    > >> polemic to push his "Sola ISO C" fundamentalism.

    >
    > >> There are many places where casts are essential: for example, to
    > >> implement polymorphism in object-oriented C programming or for type
    > >> punning. There are also many places where casts are useful: for
    > >> example, casting a uint64_t to a uint32_t is a simple way to reduce
    > >> it mod 2^32.

    >
    > > Also, it is difficult to use qsort without using cast.

    >
    > No, it's easy (see below).
    >
    > Don't make the mistake of believing Antoninus Twink. Casts are almost
    > never a good idea, and the places where they are a good idea are not
    > the places you'd think.
    >
    > Here is an example of using qsort without a cast:
    >
    > #include <stdlib.h> /* for qsort */
    > #include <string.h> /* for strcmp, used in comparison function */
    >
    > struct bigramfreq_
    > {
    >   char bigram[3];
    >   unsigned long freq;
    >
    > };
    >
    > typedef struct bigramfreq_ bigramfreq; /* the things we're sorting */
    >
    > /* comparison function */
    > int compbigramfreq(const void *vp1, const void *vp2)
    > {
    >   const bigramfreq *p1 = vp1;
    >   const bigramfreq *p2 = vp2;
    >   int diff = (p1->freq < p2->freq) - (p1->freq > p2->freq);
    >   if(diff == 0)
    >   {
    >     diff = strcmp(p1->bigram, p2->bigram);
    >   }
    >   return diff;
    >
    > }
    >
    > Once all the furniture is in place (a type to sort, and an ordering
    > function), we can define our data:
    >
    >   bigramfreq bgf[26*26] = {0};
    >
    > and, having populated it (not shown here), can sort it thusly:
    >
    >   qsort(bgf,
    >         sizeof bgf / sizeof bgf[0],
    >         sizeof bgf[0],
    >         compbigramfreq);
    >
    > I see no need for a cast anywhere here.
    >


    Yes, you are right. I spoke too soon about when I need casts.
    Using the Xt toolkit's XtMalloc function, I cast its return
    to what I need (or else get compiler warnings):
    Widget *warray = (Widget *)XtMalloc(...);

    The reason is that the writers of the Xt toolkit
    prototyped XtMalloc to return a (char *), not a (void *).

    --
    Fred K
     
    Fred, Jul 30, 2009
    #9
  10. On 30 Jul 2009 at 18:45, Richard Heathfield wrote:
    > Don't make the mistake of believing Antoninus Twink.


    Why do you have to turn everything into a question about personalities,
    Heathfield? Stick to the facts.

    > Casts are almost never a good idea, and the places where they are a
    > good idea are not the places you'd think.


    Such useless dogmatism!

    Casts are a useful implement in the C programmer's toolbox, no different
    in that respect from sizeof or strlen or #ifdef.

    Use them when they're appropriate. Don't use them when they're not
    appropriate. Why do you feel the need to turn a completely practical
    matter into a religious issue?
     
    Antoninus Twink, Jul 30, 2009
    #10
  11. "Bill Cunningham" <> writes:
    > "Mark Bluemel" <> wrote in message
    > news:...
    > On 30 July, 15:37, "Bill Cunningham" <> wrote:
    >> So many people I have noticed in their code use casts.

    >
    > Which people where?
    >
    > Perhaps you're looking at badly written code? Or at least code which
    > is not written as well as it might be...
    >
    > I meant no one in particular. Just lurking clc I see so much like
    > (void*) and such but here is one example.
    >
    > By Arnuld on clc
    >

    [snip]
    > printf("Memory allocated: %p\n", (void*)m);
    > free_my_struct(&m);
    > printf("Memory freed: %p\n", (void*)m);

    [snip]

    Arguments to variadic functions like printf are one of the few
    cases where casts are necessary and appropriate. For non-variadic
    functions, as long as a prototype is visible, the compiler knows
    the type of the parameter and can (and must) implicitly convert
    the argument to the correct type. Similar considerations apply for
    initializations, assignments, and most other expression contexts.
    But for a variadic function the compiler doesn't know the expected
    type, so you have to make sure the argument is already of the
    correct type. This often requires a cast.

    Bill, *please* fix whatever it is that's preventing you from quoting
    properly. I'm fairly sure you've been told about "Quotefix",
    which supposedly corrects the relevant bug Outlook Express.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jul 31, 2009
    #11
  12. Bill Cunningham

    Ben Pfaff Guest

    Keith Thompson <> writes:

    > "Bill Cunningham" <> writes:
    >> printf("Memory allocated: %p\n", (void*)m);
    >> free_my_struct(&m);
    >> printf("Memory freed: %p\n", (void*)m);

    > [snip]
    >
    > Arguments to variadic functions like printf are one of the few
    > cases where casts are necessary and appropriate.


    Appropriate? Sure.

    Necessary? No:

    int *m = ...;
    void *m_void = m;
    printf("Memory allocated: %p\n", m_void);
    --
    char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
    ={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa67f6aaa,0xaa9aa9f6,0x11f6},*p
    =b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
    2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}
     
    Ben Pfaff, Jul 31, 2009
    #12
  13. "Richard Heathfield" <> wrote in message
    news:...

    [snip]

    > I see no need for a cast anywhere here.


    I very much hope Richard I understood and quoted you correctly. Some of
    the code is above my head but is there ever a need for a cast?

    double Gma (double *num, int n)
    {
    int i;
    double total=0;
    for (i=0;i<n;i++)
    total +=num;
    return total/1*i;
    }

    I hope this code is correct. But on that last line I've seen a double cast.
    That isn't necessary is it?

    Bill
     
    Bill Cunningham, Jul 31, 2009
    #13
  14. Bill Cunningham

    spinoza1111 Guest

    On Jul 30, 10:37 pm, "Bill Cunningham" <> wrote:
    >     So many people I have noticed in their code use casts.  Something
    > Richard Heathfield said once very much sticks out in my mind. If code is
    > written properly there is no need (or rarely) for casts. Why do they seem to
    > be used so much then?
    >
    > Bill


    Lo! Groans Richard Heathfield, descending from the sky
    "Casts considered harmful": the folk they dare not ask why.
    He is dres't in priestly Robes but he hath dirty Feet
    And we need not consider him a Dijkstra paraclete.
    He's more a parrot or perhaps a parakeet:
    He hath learned many a seeming saw which he transmits as the Law
    But is not able to think beyond a lower bound lower than his Maw.
    Excessive casting may be a sign of deficiency
    Or it may be Genius' fate to make a velvet glove out of swinish C.
    Int to void or void to int or void to byte array
    Try what cast you like this isn't gold...it's lead that you assay.
     
    spinoza1111, Jul 31, 2009
    #14
  15. Fred <> writes:
    [...]
    > Yes, you are right. I spoke too soon about when I need casts.
    > Using the Xt toolkit's XtMalloc function, I cast its return
    > to what I need (or else get compiler warnings):
    > Widget *warray = (Widget *)XtMalloc(...);
    >
    > The reason is that the writers of the Xt toolkit
    > prototyped XtMalloc to return a (char *), not a (void *).


    I suspect that Xt was originally written before ANSI C became
    widespread, so it wasn't possible to assume that all compilers
    would support void*.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jul 31, 2009
    #15
  16. Ben Pfaff <> writes:
    > Keith Thompson <> writes:
    >
    >> "Bill Cunningham" <> writes:
    >>> printf("Memory allocated: %p\n", (void*)m);
    >>> free_my_struct(&m);
    >>> printf("Memory freed: %p\n", (void*)m);

    >> [snip]
    >>
    >> Arguments to variadic functions like printf are one of the few
    >> cases where casts are necessary and appropriate.

    >
    > Appropriate? Sure.
    >
    > Necessary? No:
    >
    > int *m = ...;
    > void *m_void = m;
    > printf("Memory allocated: %p\n", m_void);


    Right, good point.

    Here the initialization of m_void supplies the implicit conversion.
    In this particular case, though the object seems to exist only for
    the purpose of avoiding the cast. So yes, it's appropriate but
    not entirely necessary.

    Richard Heathfield showed an example of a qsort comparison function
    which refers to its arguments several times. In that example,
    copying the parameters to local variables (and incidentally
    implicitly converting them from void*) made sense; the equivalent
    code with casts would have been substantially more verbose.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jul 31, 2009
    #16
  17. Bill Cunningham

    Phil Carmody Guest

    Keith Thompson <> writes:
    > Ben Pfaff <> writes:
    >> Keith Thompson <> writes:
    >>
    >>> "Bill Cunningham" <> writes:
    >>>> printf("Memory allocated: %p\n", (void*)m);
    >>>> free_my_struct(&m);
    >>>> printf("Memory freed: %p\n", (void*)m);
    >>> [snip]
    >>>
    >>> Arguments to variadic functions like printf are one of the few
    >>> cases where casts are necessary and appropriate.

    >>
    >> Appropriate? Sure.
    >>
    >> Necessary? No:
    >>
    >> int *m = ...;
    >> void *m_void = m;
    >> printf("Memory allocated: %p\n", m_void);

    >
    > Right, good point.
    >
    > Here the initialization of m_void supplies the implicit conversion.
    > In this particular case, though the object seems to exist only for
    > the purpose of avoiding the cast. So yes, it's appropriate but
    > not entirely necessary.
    >
    > Richard Heathfield showed an example of a qsort comparison function
    > which refers to its arguments several times. In that example,
    > copying the parameters to local variables (and incidentally
    > implicitly converting them from void*) made sense; the equivalent
    > code with casts would have been substantially more verbose.


    When I saw that sort function, the first thing I thought was
    that it was too verbose using additional autos! I guess I like
    my code denser that others do.

    Phil
    --
    If GML was an infant, SGML is the bright youngster far exceeds
    expectations and made its parents too proud, but XML is the
    drug-addicted gang member who had committed his first murder
    before he had sex, which was rape. -- Erik Naggum (1965-2009)
     
    Phil Carmody, Jul 31, 2009
    #17
  18. On Thu, 30 Jul 2009 19:24:29 -0400, Bill Cunningham <>
    wrote:

    > double Gma (double *num, int n)
    > {
    > int i;
    > double total=0;
    > for (i=0;i<n;i++)
    > total +=num;
    > return total/1*i;
    > }
    > I hope this code is correct. But on that last line I've seen a double
    > cast.
    > That isn't necessary is it?


    I hope you realize that the last line is equivalent to
    return (total/1) * i;
    which is equivalent to
    return total * i;

    It looks like you're probably trying to compute the mean of a set of
    doubles, in which case it should be
    return total / i;

    (which I would write as
    return total / n;
    which is the same value, but I think the meaning is
    much more immediately apparent to the reader).

    But in none of these cases is a double cast necessary.
    A cast would be appropriate here:

    double avg(int *nums, int n)
    {
    int i;
    int total = 0;

    for (i = 0; i < n; ++i) total += nums;

    return (double)total/n;
    }

    to force the division to be computed as a double value instead of
    an int, because otherwise both operands of the division are ints.
     
    Morris Keesan, Jul 31, 2009
    #18
  19. On 30 July, 15:49, "Bill Cunningham" <> wrote:
    > "Mark Bluemel" <> wrote in message
    >
    > news:...
    > On 30 July, 15:37, "Bill Cunningham" <> wrote:
    >
    > > So many people I have noticed in their code use casts.

    >
    > Which people where?
    >
    > Perhaps you're looking at badly written code? Or at least code which
    > is not written as well as it might be...
    >
    >     I meant no one in particular. Just lurking clc I see so much like
    > (void*) and such but here is one example.
    >
    > By Arnuld on clc
    >
    > " At my job, I am working on a piece of code (around 16 source files)
    > where I have to call malloc() and free() around 20 times. Every time I
    > have to check the return value of malloc(). So I cam up with this idea
    > of writing my own malloc() and then using it. is this a good idea ?
    >
    >  #include <stdio.h>
    > #include <stdlib.h>
    >
    > enum { ARRSIZE = 10 };
    >
    > struct my_struct
    > {
    >   int i;
    >   char arrc[ARRSIZE];
    >
    > };
    >
    > void* malloc_my_struct(int num, size_t s);
    > void free_my_struct( struct my_struct** p );
    >
    > int main(void)
    > {
    >   struct my_struct* m = malloc_my_struct(1, sizeof(*m));
    >
    >   printf("Memory allocated: %p\n", (void*)m);
    >   free_my_struct(&m);
    >   printf("Memory freed:     %p\n", (void*)m);
    >   return 0;
    >
    > }
    >
    > void* malloc_my_struct(int num, size_t s)
    > {
    >   void* p = malloc(num * s);
    >
    >   if(NULL == p) fprintf(stderr, "IN: %s: Memory Exhausted, can not
    > allocate more memory\n", __func__);
    >
    >   return p;
    >
    > }
    >
    > void free_my_struct( struct my_struct** p )
    > {
    >   free(*p);
    >   *p = NULL;
    >
    > } "
    >
    > ============ OUTPUT ======================
    > [arnuld@dune programs]$ gcc -std=c99 -pedantic -Wall -Wextra xmalloc.c
    > [arnuld@dune programs]$ ./a.out
    > Memory allocated: 0x804a008
    > Memory freed:     (nil)
    > [arnuld@dune programs]$
    >
    > I got this idea actually from GNU C Library manual:http://www.gnu.org/software/hello/manual/libc/Malloc-Examples.html#Ma...
    > . 2md, I am using -std=c99 rather than -ansi flag because I am unable
    > to find any alternative to __func__ . Do you know any ?



    what casts?
     
    Nick Keighley, Jul 31, 2009
    #19
  20. On 30 July, 23:17, Antoninus Twink <> wrote:
    > On 30 Jul 2009 at 18:45, Richard Heathfield wrote:
    >
    > > Don't make the mistake of believing Antoninus Twink.

    >
    > Why do you have to turn everything into a question about personalities,
    > Heathfield? Stick to the facts.


    my irony meter imploded
     
    Nick Keighley, Jul 31, 2009
    #20
    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. =?Utf-8?B?Q2hyaXMgRGF2b2xp?=

    Web casts in ASP.Net

    =?Utf-8?B?Q2hyaXMgRGF2b2xp?=, Oct 19, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    525
    clintonG
    Oct 19, 2005
  2. cgbusch
    Replies:
    2
    Views:
    348
    Sudsy
    Jul 8, 2003
  3. Joona I Palaste

    Needless casts?

    Joona I Palaste, Apr 24, 2004, in forum: Java
    Replies:
    15
    Views:
    714
    Icemerth
    Apr 25, 2004
  4. Dan Upton

    checking casts

    Dan Upton, Nov 29, 2005, in forum: Java
    Replies:
    4
    Views:
    759
    Chris Smith
    Dec 1, 2005
  5. Wenjie

    C++ casts on zero

    Wenjie, Aug 17, 2003, in forum: C++
    Replies:
    11
    Views:
    688
    Wenjie
    Aug 24, 2003
Loading...

Share This Page