473,854 Members | 1,664 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 23329
pete wrote:

P.J. Plauger wrote:
But early in our cohabitation, I
discovered that he put a toilet paper roll on the dispenser
backwards from the way I did. Now I had carefully thought through
the matter and worked out which was the right way.


You do realise that it's a function of whether the roll
is above or below your elbow, and not a universal constant,
don't you ?

If it's the kind with jokes printed on the roll, then it needs to come
over the top so it will be readable.


Brian Rodenborn
Nov 14 '05 #121
CBFalconer <cb********@yah oo.com> writes:
The universal solution, not approved by some style mavens, is to
remove the roll from the dispenser and leave it on the tank. :-)
This makes it fairly easy to perform the necessary allocations.


This also avoids a potential problem wherein the roll resists
turning, thereby leading to excessive fragmentation ;-)
Nov 14 '05 #122
Joona I Palaste wrote:
Then *why* do you use parantheses in return statements? To make return
look like a function, so you can snigger when newbies think it really
*is* a function? =)


Never thought of it... You are right... I started changing all my
sourcecode...

Nov 14 '05 #123
"j" <ja**********@b ellsouth.net> wrote in message
news:Lp******** *******@bignews 3.bellsouth.net ...
1) Given that casts from malloc _are_ superfluous, why should one
cast?
Given that most whitespace _is_ superfluous, why should one use
whitespace?
(References to languages other than ISO C are off-topic for this
newsgroup
e.g., ``I do so because I program in C++ at work, so I have adopted this
habit''
-- what you do at work is really irrelevant for this newsgroup, i.e., ERT's original post)
Ever think of becoming a defense lawyer? You certainly like to stack the
deck against any answers you don't want to hear.

Actually, I've given more than one answer to this question, in this and
recent threads, that I believe meets your requirement of admissibility.
But I weary of repeating myself still more. All I'm holding out for,
and all I've asked for from the beginning, is for people to recognize
that:

1) Style rules should be developed by considering all applicable
principles and weighing their various importance *for that particular
rule*.

2) Well meaning people can have good reasons for giving different
weights and hence coming up with different style rules.
2) Why should one cast from malloc when it might suppress a warning
which might otherwise be useful? (We all forget to include headers from
time to time)


I've discussed this before, with my personal weights, too.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Nov 14 '05 #124
On Mon, 26 Jan 2004 00:29:24 GMT, in comp.lang.c , "P.J. Plauger"
<pj*@dinkumware .com> wrote:
What I find interesting about this debate is the two positions being
espoused:
Correct. But not these two. The two positions are

1) putting in malloc casts is perfectly ok, its harmless.
2) putting in malloc casts is unnecessary and therefore not ok.

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

2) Putting casts on malloc calls is stupid.
If you were in either camp, I'd rescind my remark faster than a very
fast thing on roller skates, being towed by a jag.
Those of us in the first camp are going to keep using casts,
But you're not in that camp. Youre in the "casting malloc is a good
thing" camp, as your own postings both in this thread and elsewhere
have showed.
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.
As I said, I respect you enormously. You mistake intolerance of your
one stupidity for lack of respect
A closed mind avoids the risk of having to change.


I'd not know, I don't have 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 #125
On Mon, 26 Jan 2004 11:20:58 +0200, in comp.lang.c , Papadopoulos
Giannis <ip******@inf.u th.gr> wrote:
E. Robert Tisdale wrote:
(but aren't all pointers unsigned ints? - enlight me plz)

No.


So???


So don't assume they are, or your programme will crash.
double* d = (double*)malloc (50*sizeof(doub le));


I think I wrote it first :)


I suspect that D Ritchie has that honour :-)
- though, without the leading double* which
is implied indeed...


At the risk of concealing a missed header, and making the code hard to
read. IMHO, YMMV.

--
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 #126
On Mon, 26 Jan 2004 14:23:28 GMT, in comp.lang.c , "P.J. Plauger"
<pj*@dinkumware .com> wrote:
Not in that sentence. It's a bald statement that happens to be untrue.
Now, had he said "there is no such thing aa an invalid conversion from
malloc(sizeof( double) to double*" I'd be quick to agree. But he didn't.
And precision is everything in our business.


IF you'd been following the context, it would have been abundantly
clear. But I guess we all snipped too much context.
--
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 #127
On Mon, 26 Jan 2004 22:54:26 GMT, in comp.lang.c , "P.J. Plauger"
<pj*@dinkumware .com> wrote:
"j" <ja**********@b ellsouth.net> wrote in message
news:Lp******* ********@bignew s3.bellsouth.ne t...
1) Given that casts from malloc _are_ superfluous, why should one
cast?
Given that most whitespace _is_ superfluous, why should one use
whitespace?


Igenerallydon't whenitdoesn'tma tter-haveyoueverseen webbytheway?

Joking aside, you're actually reinforcing one of the arguments against
casting. One should write for readability. Whitespace (used
approriately) improves that. Casts (used appropriately) improve that.
Casts stuck in where unnecessary do not improve that.
Ever think of becoming a defense lawyer? You certainly like to stack the
deck against any answers you don't want to hear.


This statement has a fine legal name, which escapes me for the moment.
Its something to do with trying to divert attention off the actual
point, because you don't have a rebuttal.
--
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 #128
On Mon, 26 Jan 2004 15:04:25 +0000, in comp.lang.c , Richard
Heathfield <in*****@addres s.co.uk.invalid > wrote:
E. Robert Tisdale wrote:
Richard Heathfield wrote:
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.
inexplicably, I didn't see your original post. Did ERT answer it
immediately? My reader has a "fry all tisdaleposts" setitng.

Anyway to respond to you now, sorry, but I won't retract. I've read
through his reasoning several times, and its IMHO wrong. Since he
persists in holding a wrong position, I consider him idiotic on this
point.
Note that this isn't a general position, merely on the malloc point.
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,


We've been over it many many times, and while I appreciate his work
circumstances make it much more convenient for him to cast malloc,
thats not a valid reason for advocating it to C programmers.
--
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 #129
On 26 Jan 2004 21:11:41 GMT, in comp.lang.c , mw*****@newsguy .com
(Michael Wojcik) wrote:
I suspect Mark thought you were claiming that a (non-null) void *
value *returned by malloc* could point to an area that was
incorrectly aligned for double.
Yes, since this was the topic under discussion !
I also suspect Mark did not recognize your name, or he might have
double-checked before posting
I recognised it very well indeed, and I elected to post anyway,
because I believe that when it comes to malloc casts, PJ has the
programming equivalent of a crush on a beautiful but unsuitable woman,
and is unable to see that she's stealing his money, sleeping with his
friends and using his house to sell dope from.... :-)
Of course, my suspicions may be ill-founded.


Nope!

--
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 #130

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
9752
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
10684
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
10763
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
10371
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7918
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
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
5744
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.