Doubt in concept of casting malloc

This is a discussion on Doubt in concept of casting malloc within the C Programming forums, part of the General Programming Boards category; "If the return of malloc is cast then the error which would be flagged is hidden, resulting in a difficult ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    53

    Doubt in concept of casting malloc

    "If the return of malloc is cast then the error which would be flagged is hidden, resulting in a difficult to find bug"

    the above stmt is mentioned in the reference

    <html>http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?id=1043284351&answer=1047673478</html>

    Kindly explain me this with an example

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,604
    I think you should just understand this conceptually (i.e., in the absence of a prototype, a function is assumed to have a return type of int), since this is a kind of bug that may be inconsistently demonstrated.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Where I've seen this is when I forget to include <stdlib.h>, and cast the return from malloc (doesn't matter what the data type is, apparently). Which backs up Laserlights assertion, btw.

    Normally, if I try to use malloc w/o including stdlib.h, I'd get an error from the compiler. With the cast there, I get no error, at all. The program just fails on malloc, or crashes if I haven't included any check on the return from malloc().

    Any program you're serious about, always check your return from the function call.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    2,543
    The way I read it is the using the cast without including the proper header will result in a link error or warning. Note: An compiler warning might/should happen also; but might not be very informative.

    But, not using the cast will result in a compiler warning/error if the header is not included.

    Because a compiler waring/error is preferred by most programmers over an Linker warning/error the recommended choice is not to use the cast.

    Tim S.
    Last edited by stahta01; 08-03-2010 at 05:02 PM. Reason: Added Note

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,172
    It's a minor point that has to do with how compiling/linking works.

    If you are just starting out, I wouldn't worry about it for now. It's not that important.

    If you are still interested, what happens is, in C, if you use a function without first declaring it, it's assumed to return int. And this code will fail to compile
    Code:
    SomeType *ptr = malloc(...);
    Because an int cannot be implicitly casted to a pointer.

    Adding a cast
    Code:
    SomeType *ptr = (SomeType *) malloc(...);
    allows the code to compile, because you explicitly casting the "int" to a pointer, and it will fail at link time instead, because although malloc is implicitly declared, it is not defined.

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by cyberfish View Post
    allows the code to compile, because you explicitly casting the "int" to a pointer, and it will fail at link time instead, because although malloc is implicitly declared, it is not defined.
    Actually, it would link just fine because malloc() is part of the standard library. That's even worse, because now you have a silently wrong program and no way of knowing it.

    Some modern compilers will warn if a well-known function hasn't been prototyped.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #7
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,172
    Ah I see. I thought malloc is defined inline by stdlib.h.

    That is very bad then, if sizeof(int) != sizeof(void *).

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Ohoho. If you think an int cannot be implicitly converted to a pointer then you don't much about C. People here do it all the time.
    Usually we get a warning about an integer being converted to a pointer (!).
    Even worse.
    Anyway yeah, bad.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A bug I cannot see. . . malloc issue
    By Kennedy in forum C Programming
    Replies: 16
    Last Post: 09-26-2007, 09:49 AM
  2. Alternative to malloc
    By stellastarr in forum C Programming
    Replies: 13
    Last Post: 04-30-2007, 04:10 PM
  3. malloc() & address allocation
    By santechz in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 08:08 AM
  4. basic doubt in pointer concept
    By sanju in forum C Programming
    Replies: 1
    Last Post: 10-24-2002, 11:35 PM
  5. help with malloc
    By geetee in forum C Programming
    Replies: 1
    Last Post: 10-23-2001, 12:07 PM

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