Why does the use of 'new' cause memory leaks?

后端 未结 9 1134
走了就别回头了
走了就别回头了 2020-11-22 08:25

I learned C# first, and now I\'m starting with C++. As I understand, operator new in C++ is not similar to the one in C#.

Can you explain the reason of

9条回答
  •  野性不改
    2020-11-22 08:50

    When creating object2 you're creating a copy of the object you created with new, but you're also losing the (never assigned) pointer (so there's no way to delete it later on). To avoid this, you'd have to make object2 a reference.

提交回复
热议问题