malloc casting - quote from the FAQ

This is a discussion on malloc casting - quote from the FAQ within the C Programming forums, part of the General Programming Boards category; Originally Posted by cpjust Maybe someone should update the FAQ with some of this info. From what I read on ...

  1. #16
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    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
    Location
    Toronto, ON
    Posts
    3,545
    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.

Page 2 of 2 FirstFirst 12
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. 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, 03:25 AM
  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