Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   malloc - to cast or not to cast, that is the question... (http://www.velocityreviews.com/forums/t317339-malloc-to-cast-or-not-to-cast-that-is-the-question.html)

EvilRix 02-13-2004 06:55 AM

malloc - to cast or not to cast, that is the question...
 
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



Severian 02-13-2004 07:13 AM

Re: malloc - to cast or not to cast, that is the question...
 
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

Martin Ambuhl 02-13-2004 07:45 AM

Re: malloc - to cast or not to cast, that is the question...
 
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

E. Robert Tisdale 02-13-2004 11:26 PM

Re: malloc - to cast or not to cast, that is the question...
 
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.



Martin Dickopp 02-14-2004 12:15 AM

Re: malloc - to cast or not to cast, that is the question...
 
"E. Robert Tisdale" <E.Robert.Tisdale@jpl.nasa.gov> 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

E. Robert Tisdale 02-14-2004 12:43 AM

Re: malloc - to cast or not to cast, that is the question...
 
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.


Severian 02-14-2004 02:09 AM

Re: malloc - to cast or not to cast, that is the question...
 
On Fri, 13 Feb 2004 16:43:49 -0800, "E. Robert Tisdale"
<E.Robert.Tisdale@jpl.nasa.gov> 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

Martin Ambuhl 02-14-2004 02:19 AM

Re: malloc - to cast or not to cast, that is the question...
 
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

Martin Dickopp 02-14-2004 12:08 PM

Re: malloc - to cast or not to cast, that is the question...
 
"E. Robert Tisdale" <E.Robert.Tisdale@jpl.nasa.gov> 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


All times are GMT. The time now is 06:43 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.