In what cases do I use malloc and/or new?

前端 未结 19 1677
北恋
北恋 2020-11-21 17:41

I see in C++ there are multiple ways to allocate and free data and I understand that when you call malloc you should call free and when you use the

19条回答
  •  梦谈多话
    2020-11-21 18:02

    new vs malloc()

    1) new is an operator, while malloc() is a function.

    2) new calls constructors, while malloc() does not.

    3) new returns exact data type, while malloc() returns void *.

    4) new never returns a NULL (will throw on failure) while malloc() returns NULL

    5) Reallocation of memory not handled by new while malloc() can

提交回复
热议问题