The difference between free and delete in C++

  First, free corresponds to malloc; delete corresponds to new; free is used to release the dynamic memory generated by malloc, and delete is used to release the dynamic memory space generated by new.

  new and delete are C++ operators, while malloc and free are functions in C.

  New does two things, one is to allocate memory, but to call the constructor of the class; similarly, delete will call the destructor of the class and release the memory. Whereas malloc and free just allocate and free memory.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325385808&siteId=291194637