Path: utzoo!censor!geac!sq!msb From: msb@sq.sq.com (Mark Brader) Newsgroups: comp.lang.c Subject: Re: Casting malloc() in ANSI C? Message-ID: <1989Dec19.081011.5594@sq.sq.com> Date: 19 Dec 89 08:10:11 GMT References: <24969@cup.portal.com> Reply-To: msb@sq.com (Mark Brader) Organization: SoftQuad Inc., Toronto Lines: 32 An article which, curiously, was posted to the USA only, asks: > In ANSI C, assigning a void pointer to any pointer variable is > legal, so casting the value returned by malloc() to the > appropriate pointer type is no longer necessary. As a matter of > recommended style, should the redundant cast be retained? I would advise retaining the cast for a few years until (a) there *is* such a thing as ANSI C, and (b) its use is widespread. Before that time there is too much probability of wanting to port back to older compilers. Once such porting really is not a problem, then I would strongly recommend removal of the cast. The statements foop = malloc (sizeof *foop); foolistp = malloc (n * sizeof *foolistp); contain NO MENTION of the type of *foop or *foolistp, which makes it impossible to get it wrong. Those who say "implicit type conversion is a bug, not a feature" will disagree. My opinion is that it is a feature. A followup suggested that the cast is required in C++ and should there- fore be used in C. My opinion is that C++ should "therefore" be fixed. -- Mark Brader "'A matter of opinion'[?] I have to say you are SoftQuad Inc., Toronto right. There['s] your opinion, which is wrong, utzoo!sq!msb, msb@sq.com and mine, which is right." -- Gene Ward Smith This article is in the public domain.