Thread: Is the type cast necessary when using malloc in C?

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    33

    Is the type cast necessary when using malloc in C?

    I am learning how to use malloc, and I am seeing that most people think it's necessary to make a type cast when passing the memory address to the pointer. For example, here's a code to get 16 new bytes allocated (4 ints) using malloc:

    Code:
    #include <stdlib.h>
    
    int main(){
       int *p;
       p = (int *)malloc(4*sizeof(int));
    
       return 0;
    }
    My question: is the (int *) cast on the right side of the attribution necessary? After all p is already a pointer to ints, so the pointer arithmetic should work fine even without that cast.

    Thanks

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    One word answer: No.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    33
    Thanks. I suspected, but better to cross check with the pros.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by envec83 View Post
    My question: is the (int *) cast on the right side of the attribution necessary? After all p is already a pointer to ints, so the pointer arithmetic should work fine even without that cast.

    Thanks
    More specifically: There is no pointer arithmetic going on here, so any worry about that is worrisome (at least on our part, because it makes us worry you have no idea what's going on). The point of the cast is that the right side and the left side are of different types -- p is an int*, as you say; and malloc returns a void*. In C, no cast is needed (or desired) to convert from void* to int*, so we suggest that you don't put it in. (And naturally, you cannot change the type of p just by assigning something to it, so it's not as though p magically becomes a void* even though it was declared as a int*.)

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Microsoft's C++ compiler which I use to compile C programs (extension .cpp) whines if I do not cast. So I do. But people get yelled at here for putting it in...
    Presumably because if one forgets to include the necessary prototype for malloc, any explicit cast would mask a serious error because malloc would be forced to return an int type as per default when no type is specified.
    I don't forget my includes and I still I like to cast because I like to get zero errors and zero warnings at the highest warning level.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Wouldn't it be easier to just use the .c extension? (ETA: Although there are probably other fiddly things that would break then. I don't use VS enough to know.)
    Last edited by tabstop; 06-15-2011 at 02:26 PM.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by nonoob View Post
    Microsoft's C++ compiler which I use to compile C programs (extension .cpp) whines if I do not cast.
    That's because you are compiling as C++ and not C. C doesn't need a cast, C++ does.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    No, it's because VS is stupid. The error highlighting highlights C++ errors only. I have a test project, with only .c files, and the project type is explicitly set to C, and those errors are still highlighted. The only option I'm aware of is to disable the error highlighting all together: Tools->Options->Text Editor->C/C++->Advanced->Disable Error Reporting or Disable Squiggles (the former turning off all background parsing, the latter just the display).

  10. #10
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by anduril462 View Post
    No, it's because VS is stupid. The error highlighting highlights C++ errors only. I have a test project, with only .c files, and the project type is explicitly set to C, and those errors are still highlighted.
    I take the emphasized part to mean that you have set the project to be compiled as C, yes?
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by nonoob View Post
    Microsoft's C++ compiler which I use to compile C programs (extension .cpp) whines if I do not cast. So I do. But people get yelled at here for putting it in...
    Presumably because if one forgets to include the necessary prototype for malloc, any explicit cast would mask a serious error because malloc would be forced to return an int type as per default when no type is specified.
    I don't forget my includes and I still I like to cast because I like to get zero errors and zero warnings at the highest warning level.
    Use the .c extension and specify the -Tc compiler option. Otherwise you're building C++ programs...

  12. #12
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    I myself try to stay away from VS for any pure C work. It sort of feels like using an aircraft to mow my lawn.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  13. #13
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by msh View Post
    I take the emphasized part to mean that you have set the project to be compiled as C, yes?
    Yes, that is what I meant.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. type cast problem
    By tikelele in forum C Programming
    Replies: 10
    Last Post: 10-31-2007, 09:23 PM
  2. Should I cast return value of malloc?
    By movl0x1 in forum C Programming
    Replies: 12
    Last Post: 05-30-2007, 10:22 AM
  3. type cast to a pointer....
    By rc7j in forum C++ Programming
    Replies: 2
    Last Post: 02-05-2002, 04:13 PM
  4. cast malloc() WHY?.
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 01-28-2002, 06:04 PM
  5. type cast or conversion?
    By steviecrawf in forum C Programming
    Replies: 2
    Last Post: 11-21-2001, 03:08 PM

Tags for this Thread