delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/06/10/02:58:31

From: zargon AT hotmail DOT vom (Zargon)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: malloc question - About output casting
Organization: Zargon and Zed Take Over The Universe
Message-ID: <3941c9e5.71979034@news.globalserve.net>
References: <393EFFB8 DOT 2D5AA7BB AT fujixerox DOT co DOT jp> <8hns8m$bvh$1 AT nets3 DOT rz DOT RWTH-Aachen DOT DE>
X-Newsreader: Forte Free Agent 1.11/32.235
Lines: 25
Date: Sat, 10 Jun 2000 04:55:31 GMT
NNTP-Posting-Host: 207.176.153.37
X-Complaints-To: news AT primus DOT ca
X-Trace: news2.tor.primus.ca 960612751 207.176.153.37 (Sat, 10 Jun 2000 00:52:31 EDT)
NNTP-Posting-Date: Sat, 10 Jun 2000 00:52:31 EDT
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

On 8 Jun 2000 10:24:22 GMT, Hans-Bernhard Broeker
<broeker AT physik DOT rwth-aachen DOT de> ate too many hallucinogenic mushrooms
and wrote:

>Casts between pointer types should be needed only *very* rarely, in a
>properly written C program. Whenever you catch yourself writing one,
>it's best to sit back and re-think your current strategy: chances are
>you're just doing something silly or evil. About the only exceptions
>from this rule are arguments to variadic functions (like printf())
>that have to be casted to (void *), and casts back from void* to some
>other type needed with calls through generalized function pointers
>like the qsort() comparison function. 

Even better to use C++. Even if you don't use many of its features,
the standard library algorithms include sorts that have a nicer, type
safe way to specify a comparison function (using templates) and there
is generally stronger type checking.

C++ does require a void * be cast to another pointer type, or produce
a warning or maybe an error. So you have to cast a malloc return
value, which is a hint that maybe in a C++ program you should be using
new :-)
-- 
Any sufficiently advanced magic is indistinguishable from
an Allegro-using C++ program compiled with gcc.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019