请帮我解决c ++类中的内存泄漏问题。 [英] Please help me solve memory leaks in a c++ class.

查看:64
本文介绍了请帮我解决c ++类中的内存泄漏问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好!

我最近开始学习c ++并且遇到了这个课程:

  class  ShapeList {
public
ShapeList(){}
~ShapeList(){}
void insert( const 形状和形状){
shapes.insert( new 形状(形状));
}
void print() const {
for (listType :: const_iterator it = shapes.begin(); it!= shapes.end(); ++ it){
std :: cout<< ; * [* it]<<的std :: ENDL;
}
}
私有
typedef std ::设置<式样* GT; listType;
listType形状;
};



我无法弄清楚如何修复此内存泄漏:

 shapes.insert( new 形状(形状)); 



-google只告诉我如何删除一种新对象:

  char  * str1 =   char  [ 30 ]; 
delete [] str1;



另外,我无法弄清楚如何解决这个问题:

- 如果你正在调用像void这样的函数f(ShapeList sl)会发生一些内存泄漏。解释原因并提出修正建议。

这是因为上面的内存泄漏?我无法看到其他问题,谷歌在这个主题上很少..



- 修改类ShapeList以支持流输出,即cout<< sl,其中sl是ShapeList类的实例。



感谢您的时间。

解决方案

您可以删除已由 new 运算符实例化的任何对象。问题更多的是你必须决定什么时候可以删除对象,在你的程序的最后或者在更早的时候,当你知道你将不再需要它们时。



对于第二个问题,您可能需要创建 ToString 方法的覆盖,该方法返回列表中对象的流。


避免内存泄漏的一种方法是避免使用指针。

 std :: set< shape> 



并对代码进行适当的更改。



否则,如果发生异常或代码不是线性的,使用共享指针或其他类型的智能指针确实可以帮助避免泄漏。


< blockquote>

Quote:

我无法弄清楚如何修复此内存泄漏:

ShapeList 析构函数,你必须遍历列表并删除每个 Shape 实例。





Quote:

如果要调用像void f(ShapeList sl)这样的函数,将会发生一些内存泄漏。解释原因并提出修正建议。

这是因为上面的内存泄漏?

是的。





Quote:

- 修改类ShapeList以支持流输出,即cout<< sl,其中sl是ShapeList类的一个实例。

你要么

  • 提供一个返回ShapeList的字符串表示的方法,例如:

     string ShapeList :: to_string(); 

  • 重载ShapeList的插入运算符(例如,参见< a href =https://msdn.microsoft.com/en-us/library/1z2f6c2k.aspx>在MSDN上重载<<<运算符为自己的类 [ ^ ])。

Hello!
I recently started studying c++ and came across this class:

class ShapeList {
        public:
          ShapeList() { }
          ~ShapeList() { }  
          void insert(const Shape& shape) {
                 shapes.insert(new Shape(shape));
            }
            void print () const {
            for (listType::const_iterator it = shapes.begin(); it != shapes.end(); ++it) {
            std::cout << *[*it] << std::endl;
            }
            }
            private:
            typedef std::set<Shape*> listType;
            listType shapes;
            };


I can't figure out how to fix this memory leak:

shapes.insert(new Shape(shape));


-google only tells me how to delete one kind of "new" object:

char* str1 = new char [30];
  delete [] str1;


Also, I can't figure out how to solve this questions:
- If you are calling a function like void f(ShapeList sl) some memory leaks will occur. Explain the causes and propose fixes.
Is this is because of the memory leak above? I can't see other problems and google is scarce on this topic..

- Modify the class ShapeList to support stream output, i.e. cout << sl , where sl is an instance fo class ShapeList.

Thank you for your time.

解决方案

You can delete any object that has been instantiated by the new operator. The issue is more that you have to decide when you can delete the objects, at the very end of your program or at some earlier time, when you know you will no longer need them.

For your second question you probably need to create an override of the ToString method, which returns a stream of the objects in the list.


One way to avoid memory leak os to avoid using pointers.

std::set<shape>


And make appropriate changes to the code.

Otherwise, using shared pointers or other kind of smart pointers can really help avoiding leaks particulary if an exception occurs or if the code is not linear.


Quote:

I can't figure out how to fix this memory leak:

In ShapeList destructor, you have to traverse the list and delete every Shape instance.


Quote:

If you are calling a function like void f(ShapeList sl) some memory leaks will occur. Explain the causes and propose fixes.
Is this is because of the memory leak above?

Yes.


Quote:

- Modify the class ShapeList to support stream output, i.e. cout << sl , where sl is an instance fo class ShapeList.

You either

  • provide a method returning a string representation of the ShapeList, e.g.

    string ShapeList::to_string();

or


这篇关于请帮我解决c ++类中的内存泄漏问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆