2
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?

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
it0099762
  • 23
  • 2
  • In both languages, the `malloc()` function returns a `void *`. In C, you can assign a `void *` to any other pointer type without a cast. In C++, you must cast the value to the correct type — there is no implicit conversion. Hence the difference. – Jonathan Leffler Apr 16 '21 at 08:03
  • Hey there! Welcome to stack overflow. Compile errors with C and C++ will vary depending on what compile flags you have turned on. To properly replicate your errors you need to give us the full command you are using to compile the corresponding source files. You should also include the actual compile errors that are produced. – Ben Wainwright Apr 16 '21 at 08:04
  • On the side, I kind of like that a 4-digit account leads on votes and got the acceptance. I also feel that is the most accessably phrased answer. This is somewhat a counter example to the "StackOverflow is toxic" meme. Very nice. Also cheering on @klutt for the next 200 reps... :-) – Yunnosch Apr 16 '21 at 08:23
  • I'm not convinced this one should be closed as a dupe of the old one and not the other way around. Seems no answer actually explained why C allows implicit conversions to happen. – Lundin Apr 16 '21 at 08:23

4 Answers4

4

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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
koder
  • 2,038
  • 7
  • 10
3

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

klutt
  • 30,332
  • 17
  • 55
  • 95
3

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).

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
2

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).

Lundin
  • 195,001
  • 40
  • 254
  • 396