469,288 Members | 2,357 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,288 developers. It's quick & easy.

Casting the return value of malloc() ?

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
Oct 2 '08
101 3698
CBFalconer said:
Mark McIntyre wrote:
>Ian Collins wrote:
>>>
Well it's pretty clear you either you don't understand the
concept of a simulation.

You might want to work on your english - the above doesn't
entirely make sense. Remove the word "either" and it does.

In other words you want him to clearly write:

"Well it's pretty clear you you don't understand the
concept of a simulation."

:-)

BTW, English is always capitalized.
No, it isn't. OTHERWISE, WE'D ALL HAVE TO WRITE LIKE THIS, Or Like This At
The Very Least.

But the first letter of "English" is normally a capital letter. Not always,
because there are some people who don't capitalise it. If you are
referring only to correct usage, even then there is some dispute. Chambers
suggests that an initial capital should always be used for place names and
for words derived from them. Some disagree, suggesting that words like
"english" (snooker term, meaning sneaky cue action to get twisty-turny
English-gentleman-style spin) should be drawn entirely from the lower
case.

Followups set to alt.english.usage (and there's a good example of where
"english" is correctly written with a lower case 'e').

--
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
Oct 15 '08 #101
On Tue, 07 Oct 2008 10:39:38 +1300, Ian Collins <ia******@hotmail.com>
wrote:
RocTheEngy wrote:

I said _almost_ 20 years ago. :) It was 1991...

But to understand your statement; there was a time when this
convention was correct?
(i.e. required)
Before C was standardised, void wasn't part of the language and malloc
returned char*
FAQ 7.7, at the usual places and http://c-faq.com/

And even after 1989, and 1990, conforming compilers weren't instantly
available for, and installed on, all machines. It is quite possible
the system your (Roc's) course/school was using did still require
casting malloc. OTOH it is possible that the professor had just
learned the old way and wasn't (yet?) aware it had changed.

It was a fairly common practice for quite a few years after 1990 to
code the casts, even if working on a conforming system that didn't
need them, so you were ready to (back)port to an older system . It is
also conceivable that the professor wanted to teach you to do that,
but if so he apparently didn't explain it adequately.

- formerly david.thompson1 || achar(64) || worldnet.att.net
Oct 20 '08 #102

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

231 posts views Thread by Brian Blais | last post: by
33 posts views Thread by hermit_crab67 | last post: by
35 posts views Thread by ytrama | last post: by
14 posts views Thread by Mirko | last post: by
8 posts views Thread by Neha | last post: by
17 posts views Thread by sophia.agnes | last post: by
32 posts views Thread by alex.j.k2 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.