问题描述
我了解内存泄漏的原因是因为在堆上创建了一个对象,并且未使用delete关键字对其进行清理.因此,如果在项目中始终在堆栈中初始化一个类,则该类不会发生内存泄漏.如果始终不使用new关键字初始化此类的对象,则该类是否可能发生内存泄漏?
例如,我有一个CStudent类,在我的代码中,我总是使用CStudent stu初始化此类.我有一些向量来存储CStudent的对象,但我将它们定义为vector< CStudent>.我从不使用new关键字来创建CStudent对象.
What I understand the reason for memory leak is because an object is created on the heap and is not cleaned by using the delete keyword. So if in a project a class is always initialized in the stack there will be no memory leaks for this class. Is it possible to have memory leaks for a class if the objects of this class are always initialized without the new keyword?
For example, I have a class CStudent and in my code I always use CStudent stu to initialize this class. I have some vectors to store objects of CStudent but I define them as vector<CStudent>. I never use new keyword to create a CStudent object. So is it possible to have memory leaks of CSudent objects?
推荐答案
这篇关于如果没有新关键字,是否可能发生内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!