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

Discussion in 'C Programming' started by EvilRix, Feb 13, 2004.

  1. EvilRix

    EvilRix Guest

    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
    EvilRix, Feb 13, 2004
    #1
    1. Advertising

  2. EvilRix

    Severian Guest

    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
    Severian, Feb 13, 2004
    #2
    1. Advertising

  3. 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
    Martin Ambuhl, Feb 13, 2004
    #3
  4. 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.
    E. Robert Tisdale, Feb 13, 2004
    #4
  5. "E. Robert Tisdale" <> 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
    Martin Dickopp, Feb 14, 2004
    #5
  6. 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.
    E. Robert Tisdale, Feb 14, 2004
    #6
  7. EvilRix

    Severian Guest

    On Fri, 13 Feb 2004 16:43:49 -0800, "E. Robert Tisdale"
    <> 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.

    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

    --
    Sev
    Severian, Feb 14, 2004
    #7
  8. 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 Ambuhl, Feb 14, 2004
    #8
  9. "E. Robert Tisdale" <> 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
    Martin Dickopp, Feb 14, 2004
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. MSG

    to cast or not to cast malloc ?

    MSG, Feb 6, 2004, in forum: C Programming
    Replies:
    38
    Views:
    1,056
    Dan Pop
    Feb 10, 2004
  2. John
    Replies:
    13
    Views:
    685
  3. ravi
    Replies:
    0
    Views:
    439
  4. Johs32

    to malloc or not to malloc??

    Johs32, Mar 30, 2006, in forum: C Programming
    Replies:
    4
    Views:
    313
    Captain Winston
    Mar 30, 2006
  5. kj

    to malloc or not to malloc?

    kj, Jun 4, 2009, in forum: C Programming
    Replies:
    5
    Views:
    293
Loading...

Share This Page