Do you cast the result of malloc?

This question already has an answer here:

  • Why does C++ require a cast for malloc() but C doesn't? 3 answers

  • In C, you can implicitly convert from a void* (the return type of malloc ) to any other pointer type. Therefore, the cast isn't required, and it's considered good C style to leave the cast off.

    In C++, you cannot implicitly convert from a void* to any other pointer type, so the cast would be required. That said, in C++, you should almost certainly be using new and delete rather than malloc , as they're more type-safe, play well with constructors and destructors, etc.

    Hope this helps!

    链接地址: http://www.djcxy.com/p/4870.html

    上一篇: 为什么这本书说我必须投malloc?

    下一篇: 你投了malloc的结果吗?