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

E

EvilRix

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,
 
S

Severian

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?
 
M

Martin Ambuhl

EvilRix said:
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.
 
E

E. Robert Tisdale

EvilRix said:
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.
 
M

Martin Dickopp

E. Robert Tisdale said:
Good idea. So do I.


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

E. Robert Tisdale

Martin said:
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.
 
S

Severian

Do you know what the "reason" is?


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.

WWKKWWWWWWKKWWWWWWKKWWWWWWKKWWKKWWWWKKWWWWKKWWWWWWWWKKWWWWKKWWWWWWWWWW
WWWWWWKKWWWWWWKKWWWWWWKKWWWWWWWWWWWWWWWWWWWWWWKKWWWWWWWWWWWWWWKKWWKKWW
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;;;;;;;;ttLLffttjjLLGGDDffiiiittttLL WWKKWWWWWWWW
WWKKWWWWWWWWiiii;;;;;;ii;;ttGGLLEEEEEEKKKKDDLLjjffffffff..WWWWWWKKWWWW
WWWWWWKKWWWWjjtt;;iiiiiittLLGGffLLLLDDEEDDDDGGLLffLLGGGG;;WWKKWWWWKKWW
WWKKWWWWKKWW;;iiiiiiiiiiffDDDDLLLLLLGGGGDDEEEEGGffGGGGGGttWWWWWWWWWWWW
WWWWWWWWWWWW,,iiiiiittttffDDEEDDEEEEGGEEKKKKEEffLLDDDDDDffKKWWKKWWKKWW
WWKKWWKKWW,,,,iittiiiittffGGttttttffLLGGGGGGLLjjLLDDEEEEttWWWWWWWWWWWW
WWWWWWWWiiiiiijjjjjjttffffffjjttjjGGGGGGLLGGLLGGDDDDEEEE;;WWWWKKWWKKWW
WWKKWWfftt;;LLjjffLLffffLLLLjjiittttjjLLffLLDDEEEEEEEEff;;iiWWWWWWWWWW
WWWWWW;;..ttEEttffLLGGGGLLLLffjjttttjjffLLDDEEKKKKKKKKLL,,ttttWWKKWWWW
WWWW::..,,ffEEffjjLLLLLLGGDDGGLLffLLLLLLDDEEKKKKKKEEKKDD,,::;;;;WWWWii
,:....;;ffLLEEDDttffLLGGLLLLGGEEEEEEEEKKKKKKKKEEKKEEEEEEii;;..,,,,;;tt
.....::jjGGGGDDEEffjjffLLLLffffffGGEEEEEEEEEEKKEEKKEEKKEEfftt;;::..::tt
...::;;jjDDDDGGEEGGiijjffGGffLLLLLLGGGGDDEEKKEEKKEEKKKKDDLLjjtt;;::::..
,,,,iiiiEEDDDDDDDDttttffLLLLLLGGLLGGDDEEEEEEKKEEDDKKEEDDffjjLLGGtt,,..
,,;;ttffDDEEDDDDEELLttjjLLLLLLGGLLDDEEEEEEEEEEDDDDKKDDLLffGGEEEEtt,,::
,,;;iiGGGGEEKKEEEEDDjjttffffLLLLDDGGDDDDEEGGDDDDEEGGLLjjDDEEKKLLii,,,,
;;iiiiLLEEDDEEEEDDDDffffffjjffLLGGGGDDGGLLGGGGDDLLGGLLEEEEKKGGjjii;;;;
iiiittttLLEEEEEEKKKKjjttffffjjffffffjjffffLLDD;;GGKKKKKKEEDDffttii;;;;
iiiiiittffLLGGDDEEKKDDffffffjjjjttttiittffjjiittKKKKEEEEGGLLttiiii;;;;
iiiittiiffffjjffjjLLDDEEDDLLttjjjjjjttjjttffKKKKDDGGGGLLLLttttiiii;;;;
iiiiiittttjjffjjffffffLLDDEEEELLffffjjttDDKKKKDDGGGGffffffttiiii;;;;ii
iiiiiittttttjjjjjjjjffffffLLLLGGDDffffEEEEDDGGGGLLLLffjjttiiii;;iiiiii
 
M

Martin Ambuhl

E. Robert Tisdale said:
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.
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.
 
M

Martin Dickopp

E. Robert Tisdale said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top