Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!weretis.net!feeder4.news.weretis.net!feeder2.ecngs.de!ecngs!feeder.ecngs.de!Xl.tags.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!local2.nntp.ams.giganews.com!nntp.brightview.co.uk!news.brightview.co.uk.POSTED!not-for-mail NNTP-Posting-Date: Wed, 27 Jul 2011 01:24:59 -0500 From: Gareth Owen Newsgroups: comp.lang.c Subject: Re: is there anything unstandard in the c part here References: <996dhnFhnlU1@mid.individual.net> <6e8108d6-cf53-4d7c-940d-8a0dcad1315b@y24g2000yqb.googlegroups.com> Date: Wed, 27 Jul 2011 07:24:59 +0100 Message-ID: <87mxg01br8.fsf@gmail.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:2N3e7r4V5sI/41MIJDjvpFCRqNo= MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Lines: 63 X-Usenet-Provider: http://www.giganews.com X-Trace: sv3-HQfKSWejElVL2o2Fa8ZOKWMQPev+NiLdZJnnqjpfns2fdn0wbQZ5+YKG3o+n/QK+ifso68ur9kSrtHc!Rn1ZkUYdV4QadbBYKonDgEl+GdWHF8dEO9+ELTMndeSa8cfcQEgNELiVFKRIqn732SQhVw== X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 2743 Xref: x330-a1.tempe.blueboxinc.net comp.lang.c:8764 James Kuyper writes: > What are the drawbacks? I'm not familiar with any. Lets look at the code (roughly) as given char *pa; /* yadda yadda yadda */ . . . pa = (char*) malloc(50); . . . /* code that presumably assumes 50 elements in pa[] */ Now the advice given was "if you change the type of *pa, the cast is wrong". So lets do that... With the cast ----------------------------------------------------------------------------- int *pa; /* yadda yadda yadda */ . . . pa = (char*) malloc(50); . . . /* code that presumably assumes 50 elements in pa[] */ result: compiler error. ----------------------------------------------------------------------------- Take the cast away ----------------------------------------------------------------------------- int *pa; /* yadda yadda yadda */ . . . pa = malloc(50); . . . /* code that presumably assumes 50 elements in pa[] */ result: undefined behaviour unless sizeof(int) == 1 The *failure* to cast malloc() has hidden a bug in *exactly* the case for which casting malloc() was held up as being wrong. ----------------------------------------------------------------------------- Personally, when I accidentally write buggy code, I want a compiler error rather undefined behaviour. YMMV. Yes, I know all about the pa = malloc(50 * sizeof(*pa)) idiom, thanks, and I know that there *are* drawbacks to casting malloc. But my underlying point is correct - casting malloc() is a *style* issue, and only idiots argue that their style is correct and anything else is wrong.