Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > casts

Reply
Thread Tools

casts

 
 
Bill Cunningham
Guest
Posts: n/a
 
      07-30-2009
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




 
Reply With Quote
 
 
 
 
Mark Bluemel
Guest
Posts: n/a
 
      07-30-2009
On 30 July, 15:37, "Bill Cunningham" <(E-Mail Removed)> 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...
 
Reply With Quote
 
 
 
 
Bill Cunningham
Guest
Posts: n/a
 
      07-30-2009

"Mark Bluemel" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
On 30 July, 15:37, "Bill Cunningham" <(E-Mail Removed)> 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/ma...alloc-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 ?


 
Reply With Quote
 
nicolas.sitbon
Guest
Posts: n/a
 
      07-30-2009
On 30 juil, 16:49, "Bill Cunningham" <(E-Mail Removed)> 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-
defined
manner."
so in that case, the cast is required.
 
Reply With Quote
 
Antoninus Twink
Guest
Posts: n/a
 
      07-30-2009
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.

 
Reply With Quote
 
Fred
Guest
Posts: n/a
 
      07-30-2009
On Jul 30, 10:58*am, Antoninus Twink <(E-Mail Removed)> 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.
 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      07-30-2009
Fred <(E-Mail Removed)> 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
 
Reply With Quote
 
Paul N
Guest
Posts: n/a
 
      07-30-2009
On 30 July, 15:37, "Bill Cunningham" <(E-Mail Removed)> 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.

 
Reply With Quote
 
Fred
Guest
Posts: n/a
 
      07-30-2009
On Jul 30, 11:45*am, Richard Heathfield <(E-Mail Removed)> wrote:
> Fred said:
>
>
>
>
>
> > On Jul 30, 10:58 am, Antoninus Twink <(E-Mail Removed)> 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
 
Reply With Quote
 
Antoninus Twink
Guest
Posts: n/a
 
      07-30-2009
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?

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
checking casts Dan Upton Java 4 12-01-2005 06:20 PM
Web casts in ASP.Net =?Utf-8?B?Q2hyaXMgRGF2b2xp?= ASP .Net 1 10-19-2005 09:45 PM
Epson colour casts on duotones? Bruce Robbins Digital Photography 5 05-25-2004 03:41 AM
Needless casts? Joona I Palaste Java 15 04-25-2004 10:14 PM
Re: = operator should automatically perform appropriate casts cgbusch Java 2 07-08-2003 03:58 PM



Advertisments