473,854 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

why is casting malloc a bad thing?

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*sizeo f(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 correct form?

thanks,

Brian Blais

Nov 14 '05
231 23326
Richard Heathfield wrote:
Wrong!
Tee-shirts *never* lie.

If that's a tee-shirt slogan, it simply proves my point.


Stack overflow - core dumped

Best regards,
Sidney

Nov 14 '05 #61
Richard Heathfield wrote:
I will be perfectly willing (as my posting history shows)
to accept that I'm wrong
if it can be shown that I am in fact wrong.
So far, however, you have not shown me to be wrong.


You can lead a mule to water but you can't make him drink.

Nov 14 '05 #62
On Sun, 25 Jan 2004 16:18:39 GMT, in comp.lang.c , "P.J. Plauger"
<pj*@dinkumware .com> wrote:
If you write malloc calls without casts, it's not because it's
necessarily good programming practice but because your grandfather did.


You know, normally I quite respect you, you're a damn fine programmer
and so forth.

But this is the one place where you're an idiot. A complete one.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.c om/ms3/bchambless0/welcome_to_clc. html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 14 '05 #63
On Sun, 25 Jan 2004 00:51:25 GMT, in comp.lang.c , "P.J. Plauger"
<pj*@dinkumware .com> wrote:
"Mark McIntyre" <ma**********@s pamcop.net> wrote in message
news:sv******* *************** **********@4ax. com...
But its impossible for it to be incorrect. The C Standard says so. If
your h/w platform can't guarantee it, then you can't implement C
there.


Next step after nonsense -- bullshit. See 6.3.2.3, para. 7.


Next step after bullshit - irrelevancy. See 6.3.2.3 para 1.

You might also want to consider that void* is not a pointer to either
an incomplete type, or an object.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.c om/ms3/bchambless0/welcome_to_clc. html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 14 '05 #64
Mark McIntyre wrote:
You might also want to consider that void* is not a pointer to either
an incomplete type, or an object.


There are three kinds of type in C: object types, function types and
incomplete types. "void" is an incomplete type.

"The void type comprises an empty set of values; it is an incomplete
type that cannot be completed."

Jeremy.
Nov 14 '05 #65
"Mark McIntyre" <ma**********@s pamcop.net> wrote in message
news:b3******** *************** *********@4ax.c om...
On Sun, 25 Jan 2004 16:18:39 GMT, in comp.lang.c , "P.J. Plauger"
<pj*@dinkumware .com> wrote:
If you write malloc calls without casts, it's not because it's
necessarily good programming practice but because your grandfather did.


You know, normally I quite respect you, you're a damn fine programmer
and so forth.

But this is the one place where you're an idiot. A complete one.


Thanks very much -- completeness is so hard to achieve in any endeavor
in these complex times.

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.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Nov 14 '05 #66
Mark McIntyre wrote:
P.J. Plauger wrote:
If you write malloc calls without casts, it's not because it's
necessarily good programming practice but because your grandfather did.


You know, normally I quite respect you,
you're a damn fine programmer and so forth.

But this is the one place where you're an idiot. A complete one.


That's an 'Ad Hominen' argument:

http://www.don-lindsay-archive.org/s...s.html#hominem

Whether P. J. Plauger is complete or incomplete idiot
has no bearing upon whether he is correct or not. :-)

Stupid people use fallacious arguments to persuade other stupid people.
Your personal attack on P. J. Plauger is a clear signal
to all subscribers that you have lost your argument
and descended to name calling instead of withdrawing graciously.

Nov 14 '05 #67
E. Robert Tisdale wrote:
Mark McIntyre wrote:
P.J. Plauger wrote:
If you write malloc calls without casts, it's not because it's
necessarily good programming practice but because your grandfather did.

You know, normally I quite respect you,
you're a damn fine programmer and so forth.

But this is the one place where you're an idiot. A complete one.


That's an 'Ad Hominen' argument...


It may be cheeky, but it's obviously _not_ an argument ad hominem.
McIntyre does not at all "deduce" the falsity of Plauger's views
on casting the return from malloc from Plauger's (supposed) idiocy.

Allin Cottrell
Nov 14 '05 #68
E. Robert Tisdale wrote:
Richard Heathfield wrote:
I will be perfectly willing (as my posting history shows)
to accept that I'm wrong
if it can be shown that I am in fact wrong.
So far, however, you have not shown me to be wrong.


You can lead a mule to water but you can't make him drink.


You can attempt to substitute slogans for thought, but you can't convince
comp.lang.c that way.

--
Richard Heathfield : bi****@eton.pow ernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #69
Mark McIntyre wrote:
On Sun, 25 Jan 2004 16:18:39 GMT, in comp.lang.c , "P.J. Plauger"
<pj*@dinkumware .com> wrote:
If you write malloc calls without casts, it's not because it's
necessarily good programming practice but because your grandfather did.


You know, normally I quite respect you, you're a damn fine programmer
and so forth.

But this is the one place where you're an idiot. A complete one.


If you're wise, Mark, you'll retract that. Mr Plauger's statement, quoted
above, is IMHO erroneous, but I don't think it's legitimate to call him an
idiot.

He has already stated for the benefit of this newsgroup that (what I
consider to be) his unusual circumstances make it sensible for him to make
his code capable of being compiled under both C and C++; given his
reputation, it makes sense to take him at his word (although that does not
necessarily mean it's sensible to emulate him blindly, in this case; we're
not /all/ writing standard libraries for C and C++ compilers, after all).

I am not forced into the position of choosing which of you is right about
malloc (for I have my own opinion on that, and I like to think that it's an
informed opinion); and thank heaven for that, when both sides are using
words like "idiot" and "nonsense". I dread to think what must be going
through the newbies' heads as they read your exchange with Mr Plauger.

--
Richard Heathfield : bi****@eton.pow ernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #70

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

Similar topics

33
2300
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
2721
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
32
2403
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:
101
4381
by: Tinkertim | last post by:
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;
0
9901
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
9751
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11027
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10682
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10761
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
7082
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5743
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4563
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
2
4159
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.