Why use new and delete at all?

后端 未结 8 1966
闹比i
闹比i 2020-12-06 19:25

I\'m new to C++ and I\'m wondering why I should even bother using new and delete? It can cause problems (memory leaks) and I don\'t get why I shouldn\'t just initialize a va

8条回答
  •  既然无缘
    2020-12-06 19:51

    Another reason may be if you are explicitly calling an external library or API with a C-style interface. Setting up a callback in such cases often means context data must be supplied and returned in the callback, and such an interface usually provides only a 'simple' void* or int*. Allocating an object or struct with new is appropriate for such actions, (you can delete it later in the callback, should you need to).

提交回复
热议问题