delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/07/20/07:25:42

From: "Michael Stewart" <mike AT reggin DOT freeserve DOT co DOT uk>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: A Structured Problem ?
Date: Tue, 20 Jul 1999 11:49:03 +0100
Organization: (Posted via) Netcom Internet Ltd.
Lines: 43
Message-ID: <7n1k6v$2m9$1@taliesin.netcom.net.uk>
References: <7mv0te$gq2$1 AT news6 DOT svr DOT pol DOT co DOT uk> <Pine DOT SUN DOT 3 DOT 96 DOT 990719090429 DOT 29235A-100000 AT silver DOT cs DOT umanitoba DOT ca> <7n09p0$9db$1 AT news5 DOT svr DOT pol DOT co DOT uk> <7n1c6l$14s$1 AT taliesin DOT netcom DOT net DOT uk> <379450DC DOT E0B363B5 AT nochance DOT com>
NNTP-Posting-Host: hgty.capgemini.co.uk
X-Trace: taliesin.netcom.net.uk 932467743 2761 194.42.240.2 (20 Jul 1999 10:49:03 GMT)
X-Complaints-To: abuse AT corp DOT netcom DOT net DOT uk
NNTP-Posting-Date: 20 Jul 1999 10:49:03 GMT
X-Newsreader: Microsoft Outlook Express 4.72.3155.0
X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3155.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Edward Hill wrote in message <379450DC DOT E0B363B5 AT nochance DOT com>...
>Michael Stewart wrote:
>: The code I posted used a cast to tell the compiler that the allocated
>memory
>: will be used as float*. Ed didn't use a cast.
>
>It's good practice not to cast, malloc doesn't require a cast and a cast
>can sometimes mask errors.
>
>: malloc returns void* and by using a cast it'll prevent the compiler
>: complaining about converting between types. It should be quite safe to
>use
>: the memory without the cast though.
>
>If the comiler is complaining about not casting malloc's return, this
>is one of the cases where casting it will mask an error.
>If my compiler moans about not casting the return of malloc it's
>because I have forgotten to #include <stdlib.h>
>
>You should only ever need to cast malloc when dynamically allocating
>a mutidimensional array.


I've just been reading the C FAQ
(ftp://rtfm.mit.edu/pub/usenet/news.answers/C-faq/faq) and it says this
about casts:

Before ANSI/ISO Standard C introduced the void * generic pointer
type, these casts were typically required to silence warnings
(and perhaps induce conversions) when assigning between
incompatible pointer types.
Under ANSI/ISO Standard C, these casts are no longer necessary,
and in fact modern practice discourages them, since they can
camouflage important warnings which would otherwise be generated
if malloc() happened not to be declared correctly; see question
7.6 above.  (However, the casts are typically seen in C code
which for one reason or another is intended to be compatible
with C++, where explicit casts from void * are required.)

So as Ed said, don't use casts in C (unless you want them to be compatible
with C++).


- Raw text -


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