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

前端 未结 19 1623
北恋
北恋 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:12

    The new and delete operators can operate on classes and structures, whereas malloc and free only work with blocks of memory that need to be cast.

    Using new/delete will help to improve your code as you will not need to cast allocated memory to the required data structure.

提交回复
热议问题