473,289 Members | 1,756 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 software developers and data experts.

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 4182
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

231
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought...
33
by: hermit_crab67 | last post by:
Can someone explain to a C newbie why this doesn't work as I expect it to work? (expectations clearly outlined in the printf statement in main routine) OS: Linux 2.4.26 GCC: 2.95.4 void...
35
by: ytrama | last post by:
Hi, I have read in one of old posting that don't cast of pointer which is returned by the malloc. I would like to know the reason. Thanks in advance, YTR
14
by: Mirko | last post by:
Hello, I'm new to this list and to Usenet in general, so please forgive (and advice) me, if I do something wrong. Anyway. I am a bit confused, because I always thought one _should_ explicitly...
8
by: Neha | last post by:
Hi all Its seems bit silly que but pleasee explan me why this error is coming ? and what will be the solution if i want void * to be intilaize by struct * and this code is puerly in C. --...
41
by: SRR | last post by:
Why is it discouraged to explicitly typecast the void pointer returned by malloc(); function? For example: { int *p; p = (int*)malloc(2*sizeof(int)); /*Explicit casting is done, therfore it...
17
by: sophia.agnes | last post by:
Hi , I was going through peter van der linden's book Expert C programming, in this book there is a section named "How and why to cast" the author then says as follows (float) 3 - it's a...
32
by: alex.j.k2 | last post by:
Hello all, I have "PRECISION" defined in the preprocessor code and it could be int, float or double, but I do not know in the code what it is. Now if I want to assign zero to a "PRECISION"...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.