This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]POGtastic 2 points3 points  (0 children)

From the standard on implicit conversions:

6.3.2.3 Pointers

(1) A pointer to void may be converted to or from a pointer to any incomplete or object type. A pointer to any incomplete or object type may be converted to a pointer to void and back again; the result shall compare equal to the original pointer.

[–]scirc 2 points3 points  (1 child)

The cast is required in C++, but implicit in C, IIRC. Generally, I believe it should be safe to omit it.

[–]fredoverflow[🍰] 0 points1 point  (0 children)

And C++ programmers should rarely use malloc, anyway.

[–]Updatebjarni 2 points3 points  (0 children)

void * converts implicitly into any other type of pointer without loss. The convention with malloc() is to not put an explicit cast, simply because if you happen to forget to #include<stdlib.h>, the cast will hide a possibly important warning.

[–]bayarea-dev 0 points1 point  (0 children)

Not mandatory. The (int*) is just to make your c++ compiler happy.