warning when using malloc

This is a discussion on warning when using malloc within the C Programming forums, part of the General Programming Boards category; 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 ...

  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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    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,189
    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 03:17 PM.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    Posts
    23,014
    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    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 04:57 AM.
    Come on, you can do it! b( ~_')

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,014
    #include <stdlib.h>
    Last edited by Elysia; 03-26-2008 at 04: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.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 07-10-2008, 04:45 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03: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, 07:39 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM

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