1. <legend id='AjgAj'><style id='AjgAj'><dir id='AjgAj'><q id='AjgAj'></q></dir></style></legend>
      <i id='AjgAj'><tr id='AjgAj'><dt id='AjgAj'><q id='AjgAj'><span id='AjgAj'><b id='AjgAj'><form id='AjgAj'><ins id='AjgAj'></ins><ul id='AjgAj'></ul><sub id='AjgAj'></sub></form><legend id='AjgAj'></legend><bdo id='AjgAj'><pre id='AjgAj'><center id='AjgAj'></center></pre></bdo></b><th id='AjgAj'></th></span></q></dt></tr></i><div id='AjgAj'><tfoot id='AjgAj'></tfoot><dl id='AjgAj'><fieldset id='AjgAj'></fieldset></dl></div>
      • <bdo id='AjgAj'></bdo><ul id='AjgAj'></ul>

      1. <small id='AjgAj'></small><noframes id='AjgAj'>

        <tfoot id='AjgAj'></tfoot>

        使用引用而不是指针是否可以解决 C++ 中的内存泄漏问题?

        Does using references instead of pointers, resolve memory leaks in C++?(使用引用而不是指针是否可以解决 C++ 中的内存泄漏问题?)

                <tbody id='O7UBk'></tbody>

              • <small id='O7UBk'></small><noframes id='O7UBk'>

                <legend id='O7UBk'><style id='O7UBk'><dir id='O7UBk'><q id='O7UBk'></q></dir></style></legend>
                  <bdo id='O7UBk'></bdo><ul id='O7UBk'></ul>
                  <i id='O7UBk'><tr id='O7UBk'><dt id='O7UBk'><q id='O7UBk'><span id='O7UBk'><b id='O7UBk'><form id='O7UBk'><ins id='O7UBk'></ins><ul id='O7UBk'></ul><sub id='O7UBk'></sub></form><legend id='O7UBk'></legend><bdo id='O7UBk'><pre id='O7UBk'><center id='O7UBk'></center></pre></bdo></b><th id='O7UBk'></th></span></q></dt></tr></i><div id='O7UBk'><tfoot id='O7UBk'></tfoot><dl id='O7UBk'><fieldset id='O7UBk'></fieldset></dl></div>
                  <tfoot id='O7UBk'></tfoot>

                • 本文介绍了使用引用而不是指针是否可以解决 C++ 中的内存泄漏问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  大多数内存泄漏发生在返回对象的指针而程序员忘记删除它时.

                  Most of memory leaks appear when a pointer of an object returned and programmer forgot to delete it.

                  例如:

                  class my_class
                  {
                    ...
                  };
                  
                  my_class* func1()
                  {
                    my_class* c = new my_class;
                    return c;
                  }
                  
                  int main()
                  {
                    my_class* var1 = func1();
                    ...
                    // Programmer forgot delete the var1: delete var1;
                    // -- or --
                    // Doesn't know 'delete[] var1;' is correct or 'delete var1;'.
                  }
                  

                  当指向创建的对象的指针而程序员忘记删除它时,会出现一些内存泄漏.

                  Some of memory leaks appear when a pointer to an object created and programmer forgot to delete it.

                  例如:

                  class my_class
                  {
                    ...
                  };
                  
                  void func2(my_class* p)
                  {
                    ...
                  }
                  
                  int main()
                  {
                    my_class* var3 = new my_class;
                  
                    func2(var3);
                  
                    // Does func2 deletes var3? Programmer doesn't know.
                    // -- or --
                    // Programmer forgot delete the var3.
                  }
                  

                  我使用了一种方法来解决内存泄漏,但在复杂情况下我不确定.

                  I use a method to resolve memory leaks but I don't sure about it in complex situations.

                  我的方法是:不使用任何指针(除了一处),只使用引用而不是指针.

                  例如:

                  class my_class
                  {
                    ...
                  };
                  
                  my_class& func1()
                  {
                    my_class* c = new my_class; // except one place.
                    return *c;
                  }
                  
                  void func2(my_class& p)
                  {
                    ...
                  }
                  
                  int main()
                  {
                    my_class& var1 = func1();
                    my_class  var2 = func1();
                  
                    my_class var3;
                    func2(var3);
                  
                    // There is nothing to forget.
                  }
                  

                  使用引用而不是指针来解决内存泄漏问题吗?

                  Does using references instead of pointers, resolve memory leaks?

                  是解决内存泄漏的好方法还是有更好的方法?

                  Is it a good method for resolving memory leaks or there are better methods?

                  这个问题的一些答案不同意下面的代码没有内存泄漏.

                  Some answer of this question don't agree the below code don't have memory leak.

                  因为是新问题,所以单独问.

                  because it is a new question, I ask it seperately.

                  class my_class
                  {
                    ...
                  };
                  
                  my_class& func()
                  {
                    my_class* c = new my_class;
                    return *c;
                  }
                  
                  int main()
                  {
                    my_class& var1 = func();
                  
                    // I think there is no memory leak.
                  }
                  

                  我在这里问:这段代码会泄漏内存吗?(引用,新建,但没有删除)

                  推荐答案

                  您还没有解决任何内存泄漏问题.如果你是新的,那么你必须删除.您所做的只是取消引用指针,它仍然需要删除.您可以通过创建本地对象并按值返回或使用智能指针来解决内存泄漏问题.100 次中有 99 次,我更喜欢按价值回报选项.

                  You haven't resolved any memory leaks. If you new, then you must delete. All you did was dereference the pointer, it still needs to be deleted. You can resolve memory leaks by creating local objects and returning by value, or using smart pointers. 99 times out of 100, I prefer the return by value option.

                  现在,像许多初学者一样,按值返回大型对象的想法可能会吓到您以性能为中心的头脑.阅读这篇以减轻您的恐惧.

                  Now, like many beginners, the idea of returning large objects by value probably scares your perf-centric mind. Read this to allay your fears.

                  这篇关于使用引用而不是指针是否可以解决 C++ 中的内存泄漏问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Constructor initialization Vs assignment(构造函数初始化 Vs 赋值)
                  Is a `=default` move constructor equivalent to a member-wise move constructor?(`=default` 移动构造函数是否等同于成员移动构造函数?)
                  Has the new C++11 member initialization feature at declaration made initialization lists obsolete?(声明时新的 C++11 成员初始化功能是否使初始化列表过时了?)
                  Order of constructor call in virtual inheritance(虚继承中构造函数调用的顺序)
                  How to use sfinae for selecting constructors?(如何使用 sfinae 选择构造函数?)
                  Initializing a union with a non-trivial constructor(使用非平凡的构造函数初始化联合)

                    <bdo id='HLTz8'></bdo><ul id='HLTz8'></ul>

                    1. <tfoot id='HLTz8'></tfoot>
                          <tbody id='HLTz8'></tbody>
                        1. <legend id='HLTz8'><style id='HLTz8'><dir id='HLTz8'><q id='HLTz8'></q></dir></style></legend>

                            <small id='HLTz8'></small><noframes id='HLTz8'>

                            <i id='HLTz8'><tr id='HLTz8'><dt id='HLTz8'><q id='HLTz8'><span id='HLTz8'><b id='HLTz8'><form id='HLTz8'><ins id='HLTz8'></ins><ul id='HLTz8'></ul><sub id='HLTz8'></sub></form><legend id='HLTz8'></legend><bdo id='HLTz8'><pre id='HLTz8'><center id='HLTz8'></center></pre></bdo></b><th id='HLTz8'></th></span></q></dt></tr></i><div id='HLTz8'><tfoot id='HLTz8'></tfoot><dl id='HLTz8'><fieldset id='HLTz8'></fieldset></dl></div>