Thread: malloc casting - quote from the FAQ

  1. #16
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Quote Originally Posted by cpjust View Post
    Maybe someone should update the FAQ with some of this info. From what I read on the FAQ, I never knew that casting malloc() could be this disasterous.
    Or maybe it could be added to the FAQ that

    1. You can't use malloc() without including stdlib.h (or declaring a function named malloc() yourself) in C99 (in full compliance mode anyway I believe).
    2. If you turn up the warning level on your C89 compiler, you are warned that the function malloc() can't be found and is assumed to return an int.

    Casting malloc() is not as disastrous as you may think. Pointless to some perhaps, but it does not violate any standard, and it requires quite a few things to go wrong for you to notice anything odd.

    If you forget to include header files, and you aren't compiling with a high warning level, you sort of expert weird bugs, don't you?

  2. #17
    and the hat of sweating
    Join Date
    Aug 2007
    Toronto, ON
    Yeah, well I always compile with the highest warning level and treat warnings as errors. Unfortunately not too many people that I see do the same.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Wiki FAQ
    By dwks in forum A Brief History of
    Replies: 192
    Last Post: 04-29-2008, 01: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. FAQ: Difference between C and C++ style casting
    By Queatrix in forum FAQ Board
    Replies: 1
    Last Post: 12-23-2006, 12:09 PM
  4. FAQ: Casting malloc?
    By Simon in forum FAQ Board
    Replies: 44
    Last Post: 10-08-2002, 02:25 AM
  5. Casting malloc?
    By Simon in forum C Programming
    Replies: 44
    Last Post: 10-08-2002, 02:25 AM