1

(Total newbie here.) From what I understand, if I code the following in C:

p = malloc(sizeof(int));

What this means is that I ask the computer to reserve 4 bytes of memory for me and assign to p the value of the address of the first byte of that 4-byte-chunk of memory.

At computer.howstuffworks.com/c29.htm we instead have:

p = (int *)malloc(sizeof(int));

How is this different?

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
  • The C compiler should perform an implicit cast from `void*` to `int*`, so explicit casting is largely unnecessary. – A Person Jan 15 '14 at 20:34

3 Answers3

2

There is a difference; see here for a full discussion: Do I cast the result of malloc?

The most important point is that casting can hide an error if you forgot to #include <stdlib.h> Without the cast, this is an error. With the cast, but without the include, C will assume that malloc() returns an int which may not be the same size as a pointer. In this case the return value of the real function will get chopped (if the pointer is longer than an int, say, on a 64-bit machine) and lead to an invalid pointer.

Community
  • 1
  • 1
TypeIA
  • 16,916
  • 1
  • 38
  • 52
  • Just to be clear: So the second line of code (which I quoted from HowStuffWorks) is ill-advised, whereas the first line is preferred? –  Jan 15 '14 at 20:05
  • 2
    @KennyLJ Yes, the first line (no cast) is preferred **for pure C only**. For C++, you *must* cast (it is an error otherwise; in C++ the cast is safe because C++ will not assume the existence of undefined functions). – TypeIA Jan 15 '14 at 20:07
  • Unless in some rare circumstances show me a real c program that will not use the c library. – this Jan 15 '14 at 20:41
  • @self It doesn't have to be a whole program, just a single compilation unit (`.c` file roughly). Modules that don't reference the C library are extremely common. Please reconsider your downvote. – TypeIA Jan 15 '14 at 20:42
  • 1
    Note that C99 did away with implcit `int` declarations, so this isn't really a consideration anymore; however, the cast should still be left off, since it's unnecessary and just adds visual clutter. The practice of casting the result of `malloc` is a holdover from K&R C; originally, the `*alloc` functions returned a `char *`, so a cast *was* necessary if the target was a different pointer type. The 1989 standard introduced `void *` specifically to act as a "generic" pointer type. – John Bode Jan 15 '14 at 21:17
0

malloc returns a void * which is okay since it is safe and automatic to convert a void * to another pointer type. The second case needlessly casts the result of malloc to an int * and you should not cast the result of malloc since it can hide error messages and is not necessary. For example if you forget to include stdlib.h you should see a warning similar to:

initialization makes pointer from integer without a cast

since in many compilers malloc will be implicitly declared to return int.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
0

malloc returns a void* by default.

in the second case you are casting it to a int*

NOTE: in C you don't have to do the cast. That is, these two statements are 100% equivalent:

char *x = malloc(100);
 char *y = (char *)malloc(100);

Conversions to and from void * are implicit in C.

gaurav5430
  • 12,934
  • 6
  • 54
  • 111