int *pi = malloc(sizeof(int));
It can do in C language but in C++ language, it has compile error.
In C++ language, it should use this way.
int *pi = (int*) malloc(sizeof(int));
what difference is malloc function in C and C++ language?
int *pi = malloc(sizeof(int));
It can do in C language but in C++ language, it has compile error.
In C++ language, it should use this way.
int *pi = (int*) malloc(sizeof(int));
what difference is malloc function in C and C++ language?
C and C++ are two different languages each with their own set of rules.
In both C and C++ malloc
returns a void*
.
In C a void*
is compatible with any pointer so it can (and should) be assigned directly. In C++ it is not compatible with anything else so it cannot be assigned directly to a non void*
.
So if you want to use malloc
in C++ (you should not) you have to cast the void*
return value to the appropriate pointer type, int*
in your example.
The difference is simply that C++ requires the cast and C does not. And it has nothing to do with malloc
. It has to do with that C++ does not allow implicit casts the same way as C does.
One reason why it's considered bad to do those casts in C is that you can do them wrong. If you pick the wrong type in C++, for instance:
int *pi = (double*) malloc(sizeof(int));
then, you'll get an error. In C, you'll get a warning and undefined behavior.
Highly related, but not a duplicate. Don't cast malloc in C
There's no difference in the malloc
function between C and C++.
The difference is the language itself: C++ has stronger type-checking than C, and doesn't allow implicit conversion from void*
to any other pointer type.
In C++ one should almost never use malloc
, not only because of this but also because it doesn't do all that the new
(and new[]
) operator is doing.
Also note that this is only one difference between the two languages, and there are many others, Which is the reason why we tell people to never tag their questions with both the C and C++ language tags if their question is only about a single language (your question is correctly tagged as it's about both).
The difference between C and C++ is that C++ doesn't allow implicit pointer conversion from void*
to other pointer types. Specifically, the rule of assignment in C (C17 6.5.16.1), emphasis mine:
the left operand has atomic, qualified, or unqualified pointer type, and (considering the type the left operand would have after lvalue conversion) one operand is a pointer to an object type, and the other is a pointer to a qualified or unqualified version of void, and the type pointed to by the left has all the qualifiers of the type pointed to by the right;
These rules of "simple assignment" also apply during parameter passing and initialization. Any code fulfilling the above constraints will lead to an implicit type conversion:
In simple assignment (=), the value of the right operand is converted to the type of the assignment expression and replaces the value stored in the object designated by the left operand.
And that's why we don't have to cast the result of malloc
in C either.
NOTE: Using malloc
in C++ is highly questionable to begin with, since among other things, it does not call constructors (and free
doesn't call destructors).