Re: Casting the return value of malloc() ?



On 2 Oct, 13:12, Richard Heathfield <r...@xxxxxxxxxxxxxxx> wrote:
polas said:

<snip>

As far as I understood it (which might be somewhat wrong) actually
casting the return value is unwanted.

If you mean "unnecessary", you're right.

Am I wrong that without
including stdlib.h the return value is int (or int * cant remember
which exactly) from malloc

In C90, whenever the compiler encounters any call to any function for which
it has not yet seen a declaration, it is required to assume that the
function returns int, even if We Know Different. (And indeed even if the
compiler knows different, which it is allowed to but not required to.)

Since you're assigning the value returned by malloc to a pointer object,
omitting the header therefore gives a type mismatch between int and
pointer, which the compiler is obliged to diagnose - UNLESS you foolishly
cast the diagnosis away.

If pointers are returned in a different way to ints, or if pointers are
longer than ints, or have completely different representations, this can
cause a very real problem.

and so not casting the return value will
provide a warning/error during compilation if you have missed this
header? Whereas casting the value will hide this problem and result in
strange behaviour

Yes, that's almost right - casting the value will hide the problem and
*may* result in strange behaviour. Or it may not. Until your boss is
watching...

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999

Right, thanks for the help clearing that one up for me :)

Nick
-----
Mesham Parallel Programming Language
www.mesham.net
.



Relevant Pages

  • Re: forwards declarations!
    ... h, long m, int w, int l); ... compiler obviously doesn't. ... LRESULT callerFunction(HWND, long, WPARAM, LPARAM), HWND ... Not quite the same as straight forwards function pointer usage. ...
    (microsoft.public.vc.language)
  • Re: Common Problems that Compilers Dont Catch
    ... where the compiler offered no help. ... Become more familiar with pointer variables and you won't be making such ... int i = p; ... Any pointer type may be converted to an integer type. ...
    (comp.lang.c)
  • Re: Inconsistent Program Results
    ... Why do you lie to the compiler? ... you define later takes an argument, a pointer to pointer to ... Under both Windows and Linux mainalways returns an int (and ... not know about the return type of malloc(). ...
    (comp.lang.c)
  • Re: Confused with malloc
    ... malloc without prototyppe gets ... interpreted as int malland this gets you code like: ... Not every compiler uses one and the same location with exactly the ... same number of bits for int and pointer to something. ...
    (comp.lang.c)
  • Re: Newbie question
    ... mainis a function that returns an int. ... can correct that by passing a pointer to the structure to the func- ... tion and operate on the structure belonging to the caller via this ... Some of the problems the compiler can tell you about if you invoke it ...
    (comp.lang.c)