delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/01/21/05:53:30

From: Edward Hill <edward_hill AT gec DOT nospam DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: casting return of malloc() (was: Re: Memory Access Problem)
Date: Thu, 21 Jan 1999 09:11:19 -0800
Organization: Easams, Engineering Systems
Lines: 34
Message-ID: <36A75FB7.2CA9@gec.nospam.com>
References: <36A13B53 DOT D1A46262 AT bellsouth DOT net> <36A67204 DOT 5AF0 AT gec DOT nospam DOT com> <25wp2.212$YL3 DOT 4320 AT typhoon DOT nycap DOT rr DOT com>
NNTP-Posting-Host: pc02372.gmsws.gecm.com
Mime-Version: 1.0
X-Mailer: Mozilla 3.01C-GECMMTL (Win95; I; 16bit)
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Matthew Conte wrote:
> 
> [ e-mail address munged to thwart evil spambots - reply to group ]
> 
> Edward Hill wrote in message <36A67204 DOT 5AF0 AT gec DOT nospam DOT com>...
> [...]
> >  You shouldn't cast the return of malloc, it was orig used
> >  to silence compiler warning messages but since ANSI/ISO and
> >  the void * type it has become redundant and sometimes masks
> >  errors.
: 
: Is this true?  It is bad practice to cast the return of malloc()? 
I've
: never seen this discussed anywhere else.
: 
: Thanks,
: Matt.

I refer you to the comp.lang.c FAQ section 7.7
7.7:    Why does some code carefully cast the values returned by malloc
        to the pointer type being allocated?

A:      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.

        References: H&S Sec. 16.1 pp. 386-7.

- Raw text -


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