On 2 Oct, 10:59, Nick Keighley <nick_keighley_nos...@hotmail.com>
wrote:
> On 2 Oct, 10:22, Tinkertim <tinker...@gmail.com> wrote:
>
>
>
> > Hi,
>
> > I have often wondered if casting the return value of malloc() (or
> > friends) actually helps anything, recent threads here suggest that it
> > does not .. so I hope to find out.
>
> > For instance :
>
> > char *tmp = NULL;
>
> > tmp = (char *) malloc(1024);
>
> > Is there any pointing in casting the return value of malloc? I see
> > many people do that, but not test the result such as :
>
> > if (tmp == (char *) NULL)
> > .. some code about malloc() failing ...
>
> > Is there any point in casting it?
>
> This is a FAQ
> FAQ 7.7b "What's wrong with casting malloc's return value?"
>
> the FAQ lives athttp://c-faq.com
>
> C++ requires a cast on the return of malloc(). But malloc()
> is unusual in C++ code (an implementation of C++ might need it).
> Some people write code that is required to compile under both C and
> C++ (eg. library writers). They also might want to cast the return
> value of malloc().
>
As far as I understood it (which might be somewhat wrong) actually
casting the return value is unwanted. Am I wrong that without
including stdlib.h the return value is int (or int * cant remember
which exactly) from malloc 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
Nick
--------
Mesham Parallel Programming Language
www.mesham.net