Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > malloc - to cast or not to cast, that is the question...

Reply
Thread Tools

malloc - to cast or not to cast, that is the question...

 
 
EvilRix
Guest
Posts: n/a
 
      02-13-2004
I've always casted the result of a call to c/malloc viz.:

int * ptr = (int *) calloc(10, sizeof(int));

However, I know this is not strictly necessary as there is always an implied
cast on the void pointer that is returned. A potential reason for not
casting was pointed out to me today by Martin Dickopp (cheers Martin). I
just wondered what the thoughts of the group were on this.

Cheers,

--

EvilRix


 
Reply With Quote
 
 
 
 
Severian
Guest
Posts: n/a
 
      02-13-2004
On Fri, 13 Feb 2004 06:55:24 -0000, "EvilRix"
<evilrix@[@@@]hotmail.com> wrote:

>I've always casted the result of a call to c/malloc viz.:
>
>int * ptr = (int *) calloc(10, sizeof(int));
>
>However, I know this is not strictly necessary as there is always an implied
>cast on the void pointer that is returned. A potential reason for not
>casting was pointed out to me today by Martin Dickopp (cheers Martin). I
>just wondered what the thoughts of the group were on this.


Back up a few posts and you'll find two threads (one _very_ long)
discussing this very issue. There's really no reason to start this
religious war all over again.

Or, HIBT?

--
Sev
 
Reply With Quote
 
 
 
 
Martin Ambuhl
Guest
Posts: n/a
 
      02-13-2004
EvilRix wrote:

> I've always casted the result of a call to c/malloc viz.:
>
> int * ptr = (int *) calloc(10, sizeof(int));
>
> However, I know this is not strictly necessary as there is always an implied
> cast on the void pointer that is returned. A potential reason for not
> casting was pointed out to me today by Martin Dickopp (cheers Martin). I
> just wondered what the thoughts of the group were on this.


This is old ground too often gone over. If you really cared what people
here thought, you would have followed the newsgroup, checked the
backtraffic (groups.google.com is good for this), and check the FAQ.

The canonical comp.lang.c approach to the above is, after #including <stdlib.h>
int *ptr = calloc(10, sizeof *ptr);
with appropriate checks afterward to be sure that calloc succeeded.
Something like
if (!ptr) { /* code to handle failure */ }
Using calloc instead of malloc makes sense only if the all-bits-zero result
is important to you, and be sure that you are not better served with
int array[10] = {0};
The magic constant '10' above should probably be moved to a #define, and,
should you use any of malloc, calloc, realloc, remember to free the space
when done.



--
Martin Ambuhl
 
Reply With Quote
 
E. Robert Tisdale
Guest
Posts: n/a
 
      02-13-2004
EvilRix wrote:

> I've always casted the result of a call to c/malloc viz.:
>
> int* ptr = (int*)calloc(10, sizeof(int));


Good idea. So do I.

> However, I know this is not strictly necessary as
> there is always an implied cast on the void pointer that is returned.
> A potential reason for not casting was pointed out to me today
> by Martin Dickopp (cheers Martin).


I don't know what reason Martin Dickopp gave to you
but it was probably bogus.

> I just wondered what the thoughts of the group were on this.


 
Reply With Quote
 
Martin Dickopp
Guest
Posts: n/a
 
      02-14-2004
"E. Robert Tisdale" <(E-Mail Removed)> writes:

> EvilRix wrote:
>
>> I've always casted the result of a call to c/malloc viz.:
>> int* ptr = (int*)calloc(10, sizeof(int));

>
> Good idea. So do I.
>
>> However, I know this is not strictly necessary as
>> there is always an implied cast on the void pointer that is returned.
>> A potential reason for not casting was pointed out to me today
>> by Martin Dickopp (cheers Martin).

>
> I don't know what reason Martin Dickopp gave to you
> but it was probably bogus.


Hello EvilRix,

Do you notice how strangely convinced E. Robert Tisdale is that the
reason is bogus, despite his claim of not knowing what it is? Draw
your own conclusions.

Martin
 
Reply With Quote
 
E. Robert Tisdale
Guest
Posts: n/a
 
      02-14-2004
Martin Dickopp wrote:

> E. Robert Tisdale writes:
>
>>I don't know what reason Martin Dickopp gave to you
>>but it was probably bogus.

>
> Hello EvilRix,
>
> Do you notice how strangely convinced E. Robert Tisdale is that
> the reason is bogus,
> despite his claim of not knowing what it is?


Do you know what the "reason" is?

> Draw your own conclusions.


My conclusion is that
something that calls itself EvilRix is your troll mate.

You can continue to feed it if you like but count your fingers.

 
Reply With Quote
 
Severian
Guest
Posts: n/a
 
      02-14-2004
On Fri, 13 Feb 2004 16:43:49 -0800, "E. Robert Tisdale"
<(E-Mail Removed)> wrote:

>Martin Dickopp wrote:
>
>> E. Robert Tisdale writes:
>>
>>>I don't know what reason Martin Dickopp gave to you
>>>but it was probably bogus.

>>
>> Hello EvilRix,
>>
>> Do you notice how strangely convinced E. Robert Tisdale is that
>> the reason is bogus,
>> despite his claim of not knowing what it is?

>
>Do you know what the "reason" is?
>
>> Draw your own conclusions.

>
>My conclusion is that
>something that calls itself EvilRix is your troll mate.
>
>You can continue to feed it if you like but count your fingers.


Please ignore the troll.

WWKKWWWWWWKKWWWWWWKKWWWWWWKKWWKKWWWWKKWWWWKKWWWWWW WWKKWWWWKKWWWWWWWWWW
WWWWWWKKWWWWWWKKWWWWWWKKWWWWWWWWWWWWWWWWWWWWWWKKWW WWWWWWWWWWWWKKWWKKWW
WWKKWWWWKKWWWWWWWWKKWWWWWWWW.. WWWW WWWWWWKKWWKKWWKKWWWWWWWWWW
WWWWWWWWWWWWKKWWWWWWWWWW WWWWWWWWWWWWWWKKWWWWKK
WWKKWWKKWWWWWWWWKKWWWW WWWWKKWWKKWWWWWWWWWW
WWWWWWWWWWKKWWWWWW WWWWWWWWWWKKWWKKWW
WWKKWWKKWWWWKKWW WWWWKKWWWWWWWWWW
WWWWWWWWWWWWWW.. ,,::::.. WWWWWWWWKKWWWW
WWKKWWWWKKWWWW ,,iittttii,,::.. WWWWKKWWWWWWKK
WWWWWWWWWWKK ,,;;;;iittii;;,,,,,,:: ..,, WWWWKKWWWWWW
WWKKWWKKWWWW .. ::;;;;,,;;,,,,,,,,::.. :::: WWWWWWWWKKWW
WWWWWWWWWWWW ,,;;,,,,,,,,,,,,,,:: ,,.. ..KKWWWWWWWW
WWKKWWKKWW ..,,;;;;,,,,,,,,,,,,::.. ..,,,, ..WWKKWWKKWW
WWWWWWWWWW ::;;;;ii;;,,,,::,,::::::.. ::::;; WWWWWWWWWW
WWKKWWWWWW ..iiiiii;;,,,,,,::,,::,,::,,,,;;;;.. WWKKWWKKWW
WWWWWWKK ..;;iiiiii;;;;,,;;,,,,,,;;iiiiii;;.. WWWWWWWWWW
WWKKWWWW ,,;;iiiiii;;;;,,,,,,,,,,;;iiiiiiii.. WWWWKKWWWW
WWWWWWWW ::iiiiiiiiii;;;;;;,,,,,,;;;;;;;;ttii,, WWWWWWKK
WWKKWWKK ..;;;;iittLLGGttttttiiiiiiiittjjttiiiiii.. WWKKWWWW
WWWWWWWW ..;;;;iiffLLDDEEEEDDffttLLEEKKKKEELLfftt::.. WWWWWWWW
WWKKWWWW ..;;iittjjffffDDEEGGjjiiDDKKDDGGDDDDffjj;; WWKKWWWW
WWWWWWKKWW ..::;;;;iijjGGEEEEEEGGjj;;LLEEEEEEEEEELLjjii:: WWWWWWKK
WWKKWWWWWW ..,,;;;;iittiitttttttttt;;ffGGffffLLtt,,tttt:: WWWWKKWWWW
WWWWWWKKWWWW,,,,;;;;;;iiiiiiiiiitttt,,;;LLtttttt,, ;;ttLL,,WWKKWWWWWWWW
WWKKWWWWWWKKii,,;;ii;;;;,,;;,,iiii;;;;,,jjjjii;;;; ;;ttGG::WWWWWWWWKKWW
WWWWWWKKWWWWttiiiiii;;;;;;;;ttLLffttjjLLGGDDffiiii ttttLL WWKKWWWWWWWW
WWKKWWWWWWWWiiii;;;;;;ii;;ttGGLLEEEEEEKKKKDDLLjjff ffffff..WWWWWWKKWWWW
WWWWWWKKWWWWjjtt;;iiiiiittLLGGffLLLLDDEEDDDDGGLLff LLGGGG;;WWKKWWWWKKWW
WWKKWWWWKKWW;;iiiiiiiiiiffDDDDLLLLLLGGGGDDEEEEGGff GGGGGGttWWWWWWWWWWWW
WWWWWWWWWWWW,,iiiiiittttffDDEEDDEEEEGGEEKKKKEEffLL DDDDDDffKKWWKKWWKKWW
WWKKWWKKWW,,,,iittiiiittffGGttttttffLLGGGGGGLLjjLL DDEEEEttWWWWWWWWWWWW
WWWWWWWWiiiiiijjjjjjttffffffjjttjjGGGGGGLLGGLLGGDD DDEEEE;;WWWWKKWWKKWW
WWKKWWfftt;;LLjjffLLffffLLLLjjiittttjjLLffLLDDEEEE EEEEff;;iiWWWWWWWWWW
WWWWWW;;..ttEEttffLLGGGGLLLLffjjttttjjffLLDDEEKKKK KKKKLL,,ttttWWKKWWWW
WWWW::..,,ffEEffjjLLLLLLGGDDGGLLffLLLLLLDDEEKKKKKK EEKKDD,,::;;;;WWWWii
,:....;;ffLLEEDDttffLLGGLLLLGGEEEEEEEEKKKKKKKKEEKK EEEEEEii;;..,,,,;;tt
.....::jjGGGGDDEEffjjffLLLLffffffGGEEEEEEEEEEKKEEK KEEKKEEfftt;;::..::tt
...::;;jjDDDDGGEEGGiijjffGGffLLLLLLGGGGDDEEKKEEKKE EKKKKDDLLjjtt;;::::..
,,,,iiiiEEDDDDDDDDttttffLLLLLLGGLLGGDDEEEEEEKKEEDD KKEEDDffjjLLGGtt,,..
,,;;ttffDDEEDDDDEELLttjjLLLLLLGGLLDDEEEEEEEEEEDDDD KKDDLLffGGEEEEtt,,::
,,;;iiGGGGEEKKEEEEDDjjttffffLLLLDDGGDDDDEEGGDDDDEE GGLLjjDDEEKKLLii,,,,
;;iiiiLLEEDDEEEEDDDDffffffjjffLLGGGGDDGGLLGGGGDDLL GGLLEEEEKKGGjjii;;;;
iiiittttLLEEEEEEKKKKjjttffffjjffffffjjffffLLDD;;GG KKKKKKEEDDffttii;;;;
iiiiiittffLLGGDDEEKKDDffffffjjjjttttiittffjjiittKK KKEEEEGGLLttiiii;;;;
iiiittiiffffjjffjjLLDDEEDDLLttjjjjjjttjjttffKKKKDD GGGGLLLLttttiiii;;;;
iiiiiittttjjffjjffffffLLDDEEEELLffffjjttDDKKKKDDGG GGffffffttiiii;;;;ii
iiiiiittttttjjjjjjjjffffffLLLLGGDDffffEEEEDDGGGGLL LLffjjttiiii;;iiiiii

--
Sev
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      02-14-2004
E. Robert Tisdale wrote:
> EvilRix wrote:
>
>> I've always casted the result of a call to c/malloc viz.:
>>
>> int* ptr = (int*)calloc(10, sizeof(int));

>
>
> Good idea. So do I.


I hope "EvilRix" means something malicious, because you have just acquired
the most evil bedfellow here when ERT agrees with you.

>> However, I know this is not strictly necessary as
>> there is always an implied cast on the void pointer that is returned.
>> A potential reason for not casting was pointed out to me today
>> by Martin Dickopp (cheers Martin).

>
> I don't know what reason Martin Dickopp gave to you
> but it was probably bogus.


That Martin Dickopp often gives good advice and ERT almost never does, and
that ERT is ready to condemn MD's advice sight-unseen should give you a
clue. There is a running score going of the few times ERT has been right
about anything: the number is small.



--
Martin Ambuhl
 
Reply With Quote
 
Martin Dickopp
Guest
Posts: n/a
 
      02-14-2004
"E. Robert Tisdale" <(E-Mail Removed)> writes:

> My conclusion is that something that calls itself EvilRix is your
> troll mate.


It's a honor to be called a troll by you. Thank you!

Martin
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
to malloc or not to malloc? kj C Programming 5 06-05-2009 02:22 PM
to malloc or not to malloc?? Johs32 C Programming 4 03-30-2006 10:03 AM
free'ing malloc'd structure with malloc'd members John C Programming 13 08-02-2004 11:45 AM
to cast or not to cast malloc ? MSG C Programming 38 02-10-2004 03:13 PM



Advertisments