Re: Cast abuse



On Mon, 19 Nov 2001, Brian Stafford wrote:

> <rant>
> As a general point, I feel that when programming in C, if a cast is used, 
> something is wrong in the code.  All reasonable efforts should be made to 
> rewrite the code avoiding the cast.  Use casts only as a last resort, after 
> all a cast silences the compiler about potential coding errors.  Also 
> remember that void pointers eliminate the need for a lot of casts, e.g. code 
> like "ptr = (int *) malloc(..." is always unnecessary since malloc returns a 
> void *.  Just write "ptr = malloc(...".  The same goes for comparisons with 
> void pointers, forget the cast.
> </rant>

Well, you will HAVE TO use casts away from void * in C++, and it's
pretty reasonable to do that in C as well. You CANNOT avoid casts for
socket functions either (didn't look at your patch). If you're after
type-safe programming, go for Ada95 or even stricter languages.

Note, GCC has some warnings about cast abuse you can turn on.



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]