473,567 Members | 3,114 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4242
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*

--
Ian Collins.
Oct 6 '08 #21
Flash Gordon <s...@spam.caus eway.comwrote:
Nick Keighley wrote, On 03/10/08 09:27:
An example of someone who writes code that compiles
with both C and C++ is P.J.Plauger.
Let's not forget Ritchie and Kernighan.
Is he an imbecile or an idiot?

I *think* he was calling the customers dumbos, not the
authors of the library.
Plauger shares the view of his customers. So any criticism
of his customers must be applied to Plauger himself, if
consistency is to hold.

Here's what he had to say in 2004...

"What I find interesting about this debate is the two
positions being espoused:

1) Omitting casts on malloc calls is acceptable, but
not necessarily virtuous.

2) Putting casts on malloc calls is stupid.

Those of us in the first camp are going to keep using
casts, and we're going to keep respecting those who
don't. It would be nice if we were granted a bit of
respect in turn, but what the hell. A closed mind
avoids the risk of having to change."

--
Peter
Oct 6 '08 #22
Peter Nilsson wrote, On 06/10/08 23:03:
Flash Gordon <s...@spam.caus eway.comwrote:
>Nick Keighley wrote, On 03/10/08 09:27:
>>An example of someone who writes code that compiles
with both C and C++ is P.J.Plauger.

Let's not forget Ritchie and Kernighan.
That was because there were no compilers conforming to ANSI C at the
time so they had to use a C++ compiler to get access to certain features.
>>Is he an imbecile or an idiot?
I *think* he was calling the customers dumbos, not the
authors of the library.

Plauger shares the view of his customers. So any criticism
of his customers must be applied to Plauger himself, if
consistency is to hold.
I thought that his view was that it was the right thing for him to do it
because that was his customers' requirement.
Here's what he had to say in 2004...

"What I find interesting about this debate is the two
positions being espoused:

1) Omitting casts on malloc calls is acceptable, but
not necessarily virtuous.

2) Putting casts on malloc calls is stupid.
He missed a view some espouse...

3) Putting casts on malloc is virtuous
Those of us in the first camp are going to keep using
casts, and we're going to keep respecting those who
don't. It would be nice if we were granted a bit of
respect in turn, but what the hell. A closed mind
avoids the risk of having to change."
Well, at least some of the people who in general strongly disagree with
putting the casts in have accepted that sometimes there is a good
reason. So not everyone who is against casting has a completely closed
mind :-)
--
Flash Gordon
If spamming me sent it to sm**@spam.cause way.com
If emailing me use my reply-to address
See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/
Oct 8 '08 #23
Flash Gordon wrote:
Peter Nilsson wrote, On 06/10/08 23:03:
>Flash Gordon <s...@spam.caus eway.comwrote:
>>Nick Keighley wrote, On 03/10/08 09:27:
An example of someone who writes code that compiles
with both C and C++ is P.J.Plauger.
Let's not forget Ritchie and Kernighan.

That was because there were no compilers conforming to ANSI C at the
time so they had to use a C++ compiler to get access to certain features.
Like void!

I don't think the original K&R even mentions malloc. They do briefly
describe calloc, which is shown as returning char* so a cast is required.

--
Ian Collins.
Oct 8 '08 #24
Flash Gordon <s...@spam.caus eway.comwrote:
Peter Nilsson wrote, On 06/10/08 23:03:
Flash Gordon <s...@spam.caus eway.comwrote:
Nick Keighley wrote, On 03/10/08 09:27:
An example of someone who writes code that compiles
with both C and C++ is P.J.Plauger.
Let's not forget Ritchie and Kernighan.

That was because there were no compilers conforming to
ANSI C at the time so they had to use a C++ compiler to
get access to certain features.
<http://groups.google.com/group/comp....ea4a63a610cac?
dmode=source>
>Is he an imbecile or an idiot?
I *think* he was calling the customers dumbos, not the
authors of the library.
Plauger shares the view of his customers. So any criticism
of his customers must be applied to Plauger himself, if
consistency is to hold.

I thought that his view was that it was the right thing for
him to do it because that was his customers' requirement.
<http://groups.google.com/group/comp....11d85b17ce318?
dmode=source>

<snip>

--
Peter
Oct 8 '08 #25
On 8 Oct, 02:14, Ian Collins <ian-n...@hotmail.co mwrote:
I don't think the original K&R even mentions malloc. *
pp 143 and 167

--
Nick Keighley
Oct 8 '08 #26
Nick Keighley wrote:
On 8 Oct, 02:14, Ian Collins <ian-n...@hotmail.co mwrote:
>I don't think the original K&R even mentions malloc.

pp 143 and 167
Not in my 1979 printing. You are thinking of K&R2.

--
Ian Collins.
Oct 8 '08 #27
On 4 Oct, 18:40, Old Wolf <oldw...@inspir e.net.nzwrote:
On Oct 3, 10:27*pm,Nick Keighley<nick_k eighley_nos...@ hotmail.com>
wrote:
Yes C and C++ are different. But you can write a programs
using a LARGE subset of C that will compile with C and C++.
I know it is unfashionable to say this in clc.
But it is none the less true.

The question is, why would you want to do this?

You can write programs that compile in both C
and Fortran.
that's just dumb. Try thinking instead of just repeating dogma.
Fortran doesn't look anything like C. Well written C compiles
with C++. That is a fact.
But again, why would you?

Here's the reasons I can think of:
* 1) For curiosity's sake
* 2) You are an idiot
whatever.

--
Nick Keighley

Oct 8 '08 #28
Nick Keighley wrote:
On 4 Oct, 18:40, Old Wolf <oldw...@inspir e.net.nzwrote:
>On Oct 3, 10:27 pm,Nick Keighley<nick_k eighley_nos...@ hotmail.com>
wrote:
>>Yes C and C++ are different. But you can write a programs
using a LARGE subset of C that will compile with C and C++.
I know it is unfashionable to say this in clc.
But it is none the less true.
The question is, why would you want to do this?

You can write programs that compile in both C
and Fortran.

that's just dumb. Try thinking instead of just repeating dogma.
Fortran doesn't look anything like C. Well written C compiles
with C++. That is a fact.
No it is not. The common subset of C and C++ compiles as C and C++.
There are a number of C constructs that are not valid C++.

--
Ian Collins.
Oct 8 '08 #29
On 8 Oct, 09:09, Ian Collins <ian-n...@hotmail.co mwrote:
Nick Keighley wrote:
On 8 Oct, 02:14, Ian Collins <ian-n...@hotmail.co mwrote:
I don't think the original K&R even mentions malloc. *
pp 143 and 167

Not in my 1979 printing. *You are thinking of K&R2.
ah! missed the word "original"! And I don't have
K&R1 at my elbow

--
nick keighley
Oct 8 '08 #30

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

231
22997
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 (perhaps mistakenly) that the purpose of a void pointer was to cast into a legitimate date type. Is this wrong? Why, and what is considered to be...
33
2235
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 modify_pointer(char *); int main(int argc, char *argv) {
35
2654
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
2638
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 cast the void* pointer returned by malloc. Now I read postings saying that this is dangerous. Could somebody please clearify this?
8
9205
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. -- #include <stdlib.h> #include <stdio.h>
41
4925
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 is said to be a bad practice*/ /*Some codes*/ }
17
2204
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 type conversion and the actual bits change. if you say (float) 3.0 it is a type disambiguation,and the compiler can plant the correct bits in the first...
32
2355
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" variable, which of the following lines are correct:
0
7588
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7903
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8115
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7650
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5488
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5216
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3648
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3630
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2093
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.