Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

C allows implict casting of void* to any other kind of pointer. Hence, you never need to cast the return from a malloc if you're storing it in a pointer.

C++, however, does not allow this. So, if for some reason you're calling malloc rather than new in C++, you need to cast.



You are not the poster who said "casting malloc's return is evil", so perhaps you don't agree with that statement, but I don't see how either of the points you make lead to the conclusion that it is "evil". You say that since void* is implicitly cast to all pointer types, you never need to explicitly cast it, but why would that make an explicit cast evil?


An explicit cast of the result of malloc() can mask an error when you haven't included its proper definition in stdlib.h.





Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: