Thread: warning when using malloc

  1. #1
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286

    Question warning when using malloc

    Hi!

    For some reason, this code creates a warning:
    Code:
    int *pointer_to_int = (int*)malloc(sizeof *pointer_to_int);
    I'm using gcc, and it says warning: int *pointer_to_int = (int*)malloc(sizeof *pointer_to_int). Do you know what it means by this and what it thinks I should do about it?
    Come on, you can do it! b( ~_')

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by TriKri View Post
    Hi!

    For some reason, this code creates a warning:
    Code:
    int *pointer_to_int = (int*)malloc(sizeof *pointer_to_int);
    I'm using gcc, and it says warning: int *pointer_to_int = (int*)malloc(sizeof *pointer_to_int). Do you know what it means by this and what it thinks I should do about it?
    I have a hard time believing that's the actual message...

  3. #3
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Well first of all, you shouldn't cast the return type of malloc (I think I read that somewhere on the forum :P)
    What is the warning that it prints out just after that line?

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  4. #4
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    I get no such warning. What gcc options are you using? Like -Wall or -pendantic?

  5. #5
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Lol a simple google search reveiled:

    http://everything2.com/e2node/Castin...lloc%2528%2529


    It is highly inadvisable when using an ANSI C compiler to type cast malloc.

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by slingerland3g View Post
    Lol a simple google search reveiled:
    I'm still confused about the warning, though. I've never seen GCC just spit out the offending line without explanation.

  7. #7
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Quote Originally Posted by QuantumPete View Post
    Well first of all, you shouldn't cast the return type of malloc
    There is nothign wrong with casting the return type, it makes the code portable. While it isnt required in C, its still perfectly legitimate. You shoudl of course make sure that teh size fo the allocated block is a multiple of the sizeof the type cast to. i.e. don't do this -

    Code:
    int* pCrash = (int*)malloc(3);
    and then try to access *pCrash
    Last edited by abachler; 03-25-2008 at 02:17 PM.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    See the FAQ!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    He is probably compiling this in C++... which is why he gets warnings related to typecasting.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    This shouldn't even complain in C++, however. Void* to int* is a perfectly acceptable cast.
    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.

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Quote Originally Posted by maxorator View Post
    He is probably compiling this in C++... which is why he gets warnings related to typecasting.
    I doubt his problems are rooted in what language he is actually using, in fact. He probably forgot to include the stdlib header though, and gcc is smart enough to tell him he's making some unwise type conversions. Except he's not smart enough to tell us what exactly his compiler displayed as a diagnostic.

  12. #12
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    Quote Originally Posted by brewbuck View Post
    I have a hard time believing that's the actual message...
    Ah, damn Emacs, can't ever copy a string to the clipboard I want...

    Anyway, here's the message:
    Code:
    warning: incompatible implicit declaration of built-in function ‘malloc’
    Last edited by TriKri; 03-26-2008 at 03:57 AM.
    Come on, you can do it! b( ~_')

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    #include <stdlib.h>
    Last edited by Elysia; 03-26-2008 at 03:09 PM.
    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.

  14. #14
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    Quote Originally Posted by citizen View Post
    I doubt his problems are rooted in what language he is actually using, in fact. He probably forgot to include the stdlib header though, and gcc is smart enough to tell him he's making some unwise type conversions. Except he's not smart enough to tell us what exactly his compiler displayed as a diagnostic.
    You're right, including stdlib.h actually solved the problem. Thanks!
    Come on, you can do it! b( ~_')

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    #include <stdlib.h>
    Not stdio, stdlib.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM