delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/07/22/00:56:53

Message-ID: <3796A2F9.D273DF@vetec.com>
Date: Wed, 21 Jul 1999 23:50:01 -0500
From: Andy Goth <andygoth AT vetec DOT com>
X-Mailer: Mozilla 4.5 [en] (Win95; I)
X-Accept-Language: en
MIME-Version: 1.0
To: djgpp AT delorie DOT com
Subject: Re: A Structured Problem ?
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> <3795CD8F DOT 191A8C0E AT americasm01 DOT nt DOT com>
Reply-To: djgpp AT delorie DOT com

> Edward Hill wrote:
> 
> > It's good practice not to cast, malloc doesn't require a cast and a cast
> > can sometimes mask errors.
> 
>     How can casting mask an error?  And why doesn't malloc need a cast?

Casting can mask an error because it defeats type safety.

This is legal:

char* b = "Hello.";
int a = (int)b;

It's probably not a good idea, but the cast lets you get away with it. 
It masks the error.

Malloc doesn't need a cast in C since C doesn't complain about
converting from a void* to any other kind of pointer.  C++ does since it
needs to know what to convert from and will issue a warning if you
convert from void* (besides, you should use "new" in C++, which returns
the specific kind of pointer you want anyway--no void* there).  In C,
the cast is a waste of typing.

- Raw text -


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