Re: Help with understanding this please



"Richard" <rgrdev@xxxxxxxxx> wrote in message news:
Richard Heathfield <rjh@xxxxxxxxxxxxxxx> writes:

mdh said:

Richard...before I rest so that sleep can be calm!!, may I ask why you
have not cast the return from malloc to type "struct t_node n *).

Why on earth would I do that? What good could it possibly do?

Yeah because C++ is wrong and so was pre Ansi C.

It's an emphasis problem.
You want to warn about potentially unsafe operations, such as converting from one type to another.
However only rarely will you want to assign the result of malloc() to a void *. In this case, the warnings are gratuitous. Too many warnings are as bad as too few, because people start ignoring them, and the genuinely exceptional casts get lost in the noise.


--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

.



Relevant Pages

  • Re: unreferenced local variable
    ... > code for but getting warnings. ... Don't cast the result of malloc, ... For every malloc(), make sure to have a corresponding freecall. ... with your exercise much more. ...
    (comp.lang.cpp)
  • Re: problem with memcpy and pointers/arrays confusion - again
    ... this second method is known as an explicit conversion, or cast. ... The cast, in effect, tells the compiler: ... the malloc function. ... function taking a size_t as a parameter and returning a void pointer (i.e. ...
    (comp.lang.c)
  • Re: why is casting malloc a bad thing?
    ... In the compilers we used in the 1980s (including VAX ... compilers for IBM PCs), malloc() had type: ... Without the cast, you got ... and adds HUGE amounts of risk. ...
    (comp.lang.c)
  • Re: Casting the return value of malloc()...
    ... The major reason for not using superfluous casts, of which malloc() ... programmer's ability to treat a cast as a warning sign. ... then the compiler can catch type errors. ... The sizeof operator should never be followed immediately by '(' ...
    (comp.lang.c)
  • Re: Casting the return value of malloc()...
    ... The major reason for not using superfluous casts, of which malloc() ... programmer's ability to treat a cast as a warning sign. ... then the compiler can catch type errors. ... The sizeof operator should never be followed immediately by '(' because it is not a function. ...
    (comp.lang.c)