FAQ: Casting malloc?

This is a discussion on FAQ: Casting malloc? within the FAQ Board forums, part of the General Programming Boards category; I fully understand your point, but it just doesn't help to cast malloc(). Then you don't fully understand my point. ...

  1. #31
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    I fully understand your point, but it just doesn't help to cast malloc().
    Then you don't fully understand my point.

    You can't see the decalaration of temp but you know it's type. So if you happen to pass the wrong type to sizeof(), then what's the point in casting the return of malloc()?]
    If you pass the wrong type to sizeof then you've allocated the incorrect amount of memory. So cast or no cast your in trouble (haven't we been here before). This has nothing to do with the point I'm making. If you know you want space for 20 int's how could you possibly pass the wrong type to sizeof()?

    Yeah you could argue that you can just scroll up to temp's declaration, but then you'd never pass the wrong type to sizeof(), so what's your argument?
    My argument is nothing to do with looking at temps declaration. In fact my argument is based on the assumption that the coder has been sloppy and not looked at temps declaration. My argument is based on the coder knowing the amount of memory they need (as stated, casting is never going to help there), and what type of pointer they want to use to manoeuvre around this memory, but then selecting the wrong pointer.

    Casting doesn't help, in any way, IMO.
    Look at my example and explain from what I've written there how casting wouldn't help. How casting wouldn't help prevent you making an incorrect assumption about the pointer type you're using? It currently has two bugs based on my false assumptions. If I had included casts these bugs would have been highlighted. So how does this mean casting doesn't help in any way?
    Joe

  2. #32
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Then you don't fully understand my point.
    Allow me to take a stab at it. You are saying that the following code is wrong if I wanted to simulate an array of integers:
    Code:
    #include <stdlib.h>
    /*
    ** my_header.h declares a to be a pointer to double
    */
    #include "my_header.h"
    
    int main ( void )
    {
      a = malloc ( 24 * sizeof *a );
    
      return 0;
    }
    
    No warnings.
    As it is the compiler implicitly converts the void pointer to a double pointer, the usage is correct, but not what you wanted. If, however, you had included a type cast to int * the compiler would likely give you a warning about incompatible types:
    Code:
    #include <stdlib.h>
    /*
    ** my_header.h declares a to be a pointer to double
    */
    #include "my_header.h"
    
    int main ( void )
    {
      a = (int *)malloc ( 24 * sizeof *a );
    
      return 0;
    }
    
    C:\C\C.c(9) : warning C4133: '=' : incompatible types - from 'int *' to 'double *'
    This is a valid point, but so is the case for not using type casting. In the end, not using type casting is encouraged, but it is very largely a matter of style.

    -Prelude
    My best code is written with the delete key.

  3. #33
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Joe, after all that, I don't think that you understand your point.

    Your point should've been, "It helps not to be a sloppy programmer!".

    Then everything would make sense!

    Nuff said already!

  4. #34
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Originally posted by JoeSixpack
    I don't want to discuss such things. Go and read K&R. Do they cast?? Or look for other gurus! They will tell you all the same.
    Please Read
    *Errata for The C Programming Language, Second Edition #142

    More Gurus
    void main() and cast to malloc
    malloc question
    Pointer Casts

  5. #35
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    Your point should've been, "It helps not to be a sloppy programmer!".
    Perhaps.

    Originally posted by JoeSixpack
    I don't want to discuss such things. Go and read K&R. Do they cast?? Or look for other gurus! They will tell you all the same.
    This wasn't my quote. I was replying to some-one who posted it.

    By all means, post keep posting links. It doesn't alter the fact that it can be beneficial to cast the return value of malloc. Also I stated this in the context of using a C99 conforming compiler (right back at the start of the thread). The reason often given (and is in a few of the links you've posted) for not casting malloc is the possible implicit prototype problem. This is no longer a problem if the compiler doesn't allow implicit prototypes.
    Joe

  6. #36
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Originally posted by JoeSixpack
    This wasn't my quote. I was replying to some-one who posted it.
    My apologies for the misquote.
    Originally posted by JoeSixpack
    By all means, post keep posting links.
    Okay, one more. But I give up.
    http://groups.yahoo.com/group/c-prog/message/12179

  7. #37
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    That link is just rehashing what's already been posted here. It doesn't stop you making an error in the assumption as to what the pointer type is (you could falsely assume that ptr3 pointed to a foobar2).

    foobar *ptr3;
    /*lots of code*/

    /*I now want 20 foobar2's
    but make an error in my assumption
    as to the type of pointer ptr3 is
    and through my stupidity assume
    that it has been declared to point
    at a foobar2s*/
    ptr3 = malloc (20*sizeof(*ptr3));

    /*casting the return value of malloc
    above would have explicitly told the compiler
    what I was trying to do, and made it
    scream at me. Therefore the method
    used above is less robust*/

    Also, the poster in your link complaining about all the cutting and pasting needed has presumably not heard of macros.

    But I give up.
    So do I . Probably.
    Joe

  8. #38
    Registered User
    Join Date
    Dec 2001
    Posts
    88
    It's funny to read...

    I think noone will change their minds, so why discussing?

    I (and as I see some others) keep doing it like the gurus, and you JoeSixpack keep casting.

    I have learned that it is possible to program in a different style as the gurus, but I got to know, that it is somehow easier when you do what the gurus tell you
    Hope you don't mind my bad english, I'm Austrian!

  9. #39
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    I think noone will change their minds, so why discussing?
    Change someones mind about what? I'm not trying to convince people that they should always cast the return value of malloc; just pointing out that it can be benefical in some situations. My example showed that by using casting two bugs could have been avoided. This wasn't my opinion. It was a fact.

    I (and as I see some others) keep doing it like the gurus, and you JoeSixpack keep casting.

    I have learned that it is possible to program in a different style as the gurus, but I got to know, that it is somehow easier when you do what the gurus tell you
    That's a really intellitgent pov, Shade. As I said above I'm not trying to convince anyone that they should cast the return value of malloc. My initial post was a question, hoping to start a debate on the subject. Not provoke mindless guru fanboyism such as that demonstrated by you among others. If you'd like to point out why casting is a bad thing in the context of the example I gave, then I'm all ears (or eyes). Otherwise you're contributing nothing.
    Joe

  10. #40
    Visionary Philosopher Sayeh's Avatar
    Join Date
    Aug 2002
    Posts
    212
    I had to reinvolve myself with this-- this thread is an absurd example (yet again) of people becoming lost in the syntax issues of a language or a compiler, rather than being able to see beyond such things and get to the fascination of the logic.

    Get's old, doesn't it.

    -------------------------------------------------------------
    To JoeSixPack:

    I have yet to see any justifyable advantage to casting.
    It has nothing to do with advantages or disadvantages. This is the wrong point of view. Stop thinking it is right or wrong. Type coercsion was developed for one reason and one reason only--

    To resolve type mismatches at the preprocessor level. type coercion has nothing to do with execution.


    ---------------------------------------------------------------
    To the rest of you:

    If JoeSixPack wants to write all his code without ever using coercion-- fine. More power to this person.

    We've listed what coercion/casting is for. It's a tool, nothing more. I prefer to utilize all my tools, rather than taking the extreme stance that 'tool x is good for nothing' or 'you should never use tool z'.

    Keep on programming
    It is not the spoon that bends, it is you who bends around the spoon.

  11. #41
    Registered User
    Join Date
    Sep 2002
    Posts
    272
    *Joe Sxpack bangs his head violently against the wall*

    this thread is an absurd example (yet again) of people becoming lost in the syntax issues of a language or a compiler, rather than being able to see beyond such things and get to the fascination of the logic.
    Actually, all this thread has seemed to demonstrate is peoples inability to read.

    And that, is timeless. Nice misquote, btw.
    Joe

  12. #42
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    First off I would like to point out to everyone that says "don't type cast malloc()" that just because you don't need the type cast doesn't you shouldn't. malloc(), calloc(), and realloc() are part of the standard C memory manager. Check this out:

    Code:
    int *i = malloc(1);
    This will return a pointer to a block of memory that is not big enough to accomodate the size of an int.

    Code:
    int *i = (int *)malloc(1);
    This code still erroneous. So therefore I do not understand why people take issue with the cast (which is meaningless) if the only provision here is compatibility with older compilers. I did take note that syntax can be an issue with some compilers, however, a compiler that gives you grief over simple code such as this is not not a great compiler. If you look through my code you will always see a cast in front of all my mallocs, callocs, and reallocs only out of habbit. This is an issue of personal opinion.

  13. #43
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    int *i = malloc(1);
    And like the other absurd example given for reasons to typecast, only a fool would do this. And, just like the other example, there is no point to type casting this mistake, because with or without the typecast, you still end up with incorrect code and the type cast has no effect what so ever. Neither one produces an error. Neither one produces a warning. Neither one will fix your broken code. Like I've said: There is no reason to ever cast malloc. The only remote examples provided are so absurd they'd never happen in real life, or if they did, they'd happen by a total novice coder who wouldn't benifit either way from the cast, or without it.

    Quzah.
    Hope is the first step on the road to disappointment.

  14. #44
    Registered User
    Join Date
    Feb 2002
    Posts
    6

    Red face realloc

    By the way, can any body expain how to use realloc() function, and it is better to use a example.
    Thanx for help!

  15. #45
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789

    Re: realloc

    Originally posted by pnxi
    By the way, can any body expain how to use realloc() function, and it is better to use a example.
    Thanx for help!
    Try a search

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Wiki FAQ
    By dwks in forum A Brief History of Cprogramming.com
    Replies: 192
    Last Post: 04-29-2008, 02:17 PM
  2. malloc, calloc from the FAQ
    By salvadoravi in forum C Programming
    Replies: 10
    Last Post: 01-21-2008, 03:29 AM
  3. malloc casting - quote from the FAQ
    By salvadoravi in forum C Programming
    Replies: 16
    Last Post: 12-17-2007, 06:24 PM
  4. FAQ: Difference between C and C++ style casting
    By Queatrix in forum FAQ Board
    Replies: 1
    Last Post: 12-23-2006, 12:09 PM
  5. Casting malloc?
    By Simon in forum C Programming
    Replies: 44
    Last Post: 10-08-2002, 03:25 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21