What is the use of typecast in malloc? If I don't write the typecast in malloc then what will it return? (Why is typecasting required in malloc?)
5 Answers
I assume you mean something like this:
int *iptr = (int*)malloc(/* something */);
And in C, you do not have to (and should not) cast the return pointer from malloc
. It's a void *
and in C, it is implicitly converted to another pointer type.
int *iptr = malloc(/* something */);
Is the preferred form.
This does not apply to C++, which does not share the same void *
implicit cast behavior.

- 77,184
- 16
- 165
- 176
-
@user615929 - what do you need more clearly explained? You do not need to cast the return `void *` from `malloc` since it can be freely, and implicitly, converted to other pointer types. – wkl Feb 14 '11 at 15:39
-
char *iptr = (char*)malloc(/* something); means, it will type cast in to char ; char *iptr =malloc(/* something); after allocating the size ... how it will type cast in to char? – user615929 Feb 14 '11 at 15:48
-
u mean automatically it will type cast? – user615929 Feb 14 '11 at 15:53
-
2@user615929 - In C, pointers to `void` (`void *`) can be converted to any other pointer type without a cast, unlike pointers to other types (e.g., assigning a value of type `char *` to a variable of type `int *` *does* require an explicit cast). Thus, the value returned by `malloc` will be automatically converted to the target pointer type, no cast necessary. Note that in *very old* (pre-1989) versions of C, `malloc` returned `char *`, so an explicit cast *was* neccesary if you were assigning the result to a variable of a diffferent type. – John Bode Feb 14 '11 at 15:54
You should never cast the return value of malloc()
, in C. Doing so is:
- Unnecessary, since
void *
is compatible with any other pointer type (except function pointers, but that doesn't apply here). - Potentially dangerous, since it can hide an error (missing declaration of the function).
- Cluttering, casts are long and often hard to read, so it just makes the code uglier.
So: there are no benefits, at least three drawbacks, and thus it should be avoided.

- 391,730
- 64
- 469
- 606
-
-
The second point is wrong. If you forget to include
it does not matter whether you cast the result of malloc or not, you get a warning "implicit function declaration" (without any additional switches to most compilers). If you produce and ignore warnings - it is your problem, not the laguage, not the compiler, not the casting - it is you. The first point is a dry fact, no argue about that. All other points are only about your personal preference. – sirgeorge Mar 16 '12 at 07:08
You're not required to cast the return value of malloc
. This is discussed further in the C FAQ: http://c-faq.com/malloc/cast.html and http://c-faq.com/malloc/mallocnocast.html .

- 70,581
- 9
- 108
- 149
Just because malloc returns a void
* and since void*
has not defined size you can't apply pointer aritmetic on it. So you generally cast the pointer to the data type your allocated memory block actually points.

- 32,832
- 9
- 75
- 115
The answers are correct, I just have an advice:
- don't play with pointers - which malloc() returns - too much, cast them to a specified type asap;
- if you need to do some math with them, cast them to char*, so ptr++ will mean what you except: add 1 to it (the size of the datatype will be added, which is 1 for char).

- 3,074
- 25
- 40