|
From: <da...@ix...> - 2003-04-24 01:54:32
|
In article <3EA...@tr...>, Joseph Knapka <wra...@li...> wrote: >Could you possibly provide a reference for this rule? I have always >cast the results of malloc(), based on the advice contained in >K&R 2nd ed. pg. 142 (K&R2 claims to cover ANSI C), but (obviously) >I haven't done much C programming recently :-) Other sources I've >looked at say that it's not *necessary* to cast malloc's result, >but that it doesn't hurt. So I'm just wondering about your statement >above, which seems to be made with great conviction :-) Does the >ISO C standard recommend against casting? Errata for The C Programming Language, Second Edition http://cm.bell-labs.com/cm/cs/cbook/2ediffs.html 142: The remark about casting the return value of malloc ("the proper method is to declare ... then explicitly coerce") needs to be rewritten. The example is correct and works, but the advice is debatable in the context of the 1988-1989 ANSI/ISO standards. It's not necessary (given that coercion of void * to ALMOSTANYTYPE * is automatic), and possibly harmful if malloc, or a proxy for it, fails to be declared as returning void *. The explicit cast can cover up an unintended error. On the other hand, pre-ANSI, the cast was necessary, and it is in C++ also. 167: [the return value of malloc or calloc] "must be cast into the appropriate type" is incorrect as stated. See the remarks just above for p. 142. Not to mention, just about any thread on comp.lang.c. :-> mrc -- Mike Castle da...@ix... www.netcom.com/~dalgoda/ We are all of us living in the shadow of Manhattan. -- Watchmen fatal ("You are in a maze of twisty compiler features, all different"); -- gcc |