Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Casting the return value of malloc() ?

Reply
Thread Tools

Casting the return value of malloc() ?

 
 
Tinkertim
Guest
Posts: n/a
 
      10-02-2008
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?

Cheers,
--Tim
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      10-02-2008
Tinkertim 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?


None what so ever.

--
Ian Collins.
 
Reply With Quote
 
 
 
 
Kenny McCormack
Guest
Posts: n/a
 
      10-02-2008
In article <7ce1a426-30ed-47ee-bd72->,
Tinkertim <> 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.


Good to see we're back on familiar ground.

 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      10-02-2008
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 at http://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().


--
Nick Keighley

"Those are my principles. If you don't like them, I have others."
- Groucho Marx
 
Reply With Quote
 
polas
Guest
Posts: n/a
 
      10-02-2008
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
 
Reply With Quote
 
polas
Guest
Posts: n/a
 
      10-02-2008
On 2 Oct, 13:12, Richard Heathfield <r...@see.sig.invalid> 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
 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      10-02-2008
On 2 Oct, 12:42, polas <n...@helpforce.com> wrote:
> On 2 Oct, 10:59, Nick Keighley <nick_keighley_nos...@hotmail.com>
> wrote:
> > On 2 Oct, 10:22, Tinkertim <tinker...@gmail.com> wrote:


> > > 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().


and note the "C++" in my answer. C and C++ are different langauges
and the rules are slightly different. The C++ rules may in some
slightly obscure cases lead to C being written to C++ rules.

> 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)


int

> 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


yes

--
Nick Keighley
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      10-02-2008
Nick Keighley wrote:
>

.... snip ...
>
> 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().


Not library writers. That would be an excellent way of installing
library bugs. However some people want to sell their software,
written in C source, to imbeciles who will simply compile it and
then use it. They want this to work even though the idiots are
using a C++ compiler. After all, why have a support call? Without
it the dumbos are happy, and the selling firm makes more money.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      10-02-2008
Tinkertim <tinker...@gmail.com> wrote:
> I have often wondered if casting the return value
> of malloc() (or friends) actually helps anything,
> ...


You should first define what you mean by 'help', then
ask the more basic question of whether casts help
anything period.

--
Peter
 
Reply With Quote
 
Kenny McCormack
Guest
Posts: n/a
 
      10-03-2008
In article <8be39e8f-ccd3-4a22-9c6b->,
Nick Keighley <> wrote some good stuff,
leading up to:
....
>An example of someone who writes code that compiles with both
>C and C++ is P.J.Plauger. Is he an imbecile or an idiot?
>
>you are on the verge of a plonk


I'm not sure who is the better archetype of the senile old fool: CBF or
McBush.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Up casting and down casting Sosuke C++ 2 12-20-2009 03:24 PM
Problem with depracated casting method (down casting) Wally Barnes C++ 3 11-20-2008 05:33 AM
Casting return value to fit within constraints of error values (portability) clayne C Programming 4 01-28-2006 10:15 PM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM
Another question about inheritance (up-casting and down-casting) kevin Java 11 01-08-2005 07:11 PM



Advertisments