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

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

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

      1. <tfoot id='KgQnI'></tfoot>

        如何在 C++ 代码/项目中找到内存泄漏?

        How to find memory leak in a C++ code/project?(如何在 C++ 代码/项目中找到内存泄漏?)

          <small id='1mFnP'></small><noframes id='1mFnP'>

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

              • <legend id='1mFnP'><style id='1mFnP'><dir id='1mFnP'><q id='1mFnP'></q></dir></style></legend>
                  本文介绍了如何在 C++ 代码/项目中找到内存泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我是 Windows 平台上的 C++ 程序员.我使用的是 Visual Studio 2008.

                  I am a C++ programmer on the Windows platform. I am using Visual Studio 2008.

                  我通常会在代码中出现内存泄漏.

                  I usually end up in the code with memory leaks.

                  通常我通过检查代码来发现内存泄漏,但它很麻烦,并不总是一个好方法.

                  Normally I find the memory leak by inspecting the code, but it is cumbersome and is not always a good approach.

                  因为我买不起付费的内存泄漏检测工具,所以我希望你们能建议最好的方法来避免内存泄漏.

                  Since I can't afford a paid memory leak detection tool, I wanted you guys to suggest the best possible ways to avoid memory leaks.

                  1. 我想知道程序员如何发现内存泄漏.
                  2. 是否应该遵循任何标准或程序来确保程序中没有内存泄漏?

                  推荐答案

                  说明

                  你需要的东西

                  • 精通 C++
                  • C++ 编译器
                  • 调试器和其他调查软件工具

                  了解操作员基础知识.C++ 运算符 new 分配堆内存.delete 操作符释放堆内存.对于每个 new,您应该使用 delete 以便释放您分配的相同内存:

                  Understand the operator basics. The C++ operator new allocates heap memory. The delete operator frees heap memory. For every new, you should use a delete so that you free the same memory you allocated:

                  char* str = new char [30]; // Allocate 30 bytes to house a string.
                  
                  delete [] str; // Clear those 30 bytes and make str point nowhere.
                  

                  2

                  仅当您已删除时才重新分配内存.在下面的代码中,str 通过第二次分配获得一个新地址.第一个地址不可挽回地丢失了,它指向的 30 个字节也是如此.现在它们不可能被释放,而且你有内存泄漏:

                  2

                  Reallocate memory only if you've deleted. In the code below, str acquires a new address with the second allocation. The first address is lost irretrievably, and so are the 30 bytes that it pointed to. Now they're impossible to free, and you have a memory leak:

                  char* str = new char [30]; // Give str a memory address.
                  
                  // delete [] str; // Remove the first comment marking in this line to correct.
                  
                  str = new char [60]; /* Give str another memory address with
                                                                      the first one gone forever.*/
                  
                  delete [] str; // This deletes the 60 bytes, not just the first 30.
                  

                  3

                  注意那些指针分配.每个动态变量(堆上分配的内存)都需要与一个指针相关联.当动态变量与其指针脱离关联时,将无法擦除.同样,这会导致内存泄漏:

                  3

                  Watch those pointer assignments. Every dynamic variable (allocated memory on the heap) needs to be associated with a pointer. When a dynamic variable becomes disassociated from its pointer(s), it becomes impossible to erase. Again, this results in a memory leak:

                  char* str1 = new char [30];
                  
                  char* str2 = new char [40];
                  
                  strcpy(str1, "Memory leak");
                  
                  str2 = str1; // Bad! Now the 40 bytes are impossible to free.
                  
                  delete [] str2; // This deletes the 30 bytes.
                  
                  delete [] str1; // Possible access violation. What a disaster!
                  

                  4

                  小心本地指针.您在函数中声明的指针分配在堆栈上,但它指向的动态变量分配在堆上.如果你不删除它,它会在程序退出函数后持续存在:

                  4

                  Be careful with local pointers. A pointer you declare in a function is allocated on the stack, but the dynamic variable it points to is allocated on the heap. If you don't delete it, it will persist after the program exits from the function:

                  void Leak(int x){
                  
                  char* p = new char [x];
                  
                  // delete [] p; // Remove the first comment marking to correct.
                  
                  }
                  

                  5

                  注意删除"后的方括号.单独使用 delete 来释放单个对象.使用带有方括号的 delete [] 来释放堆数组.不要做这样的事情:

                  5

                  Pay attention to the square braces after "delete." Use delete by itself to free a single object. Use delete [] with square brackets to free a heap array. Don't do something like this:

                  char* one = new char;
                  
                  delete [] one; // Wrong
                  
                  char* many = new char [30];
                  
                  delete many; // Wrong!
                  

                  6

                  如果泄漏尚未允许 - 我通常会通过 deleaaker 寻找它(在此处查看:http://deleaker.com).

                  这篇关于如何在 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(使用非平凡的构造函数初始化联合)

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

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

                        • <bdo id='s3pXQ'></bdo><ul id='s3pXQ'></ul>
                          • <legend id='s3pXQ'><style id='s3pXQ'><dir id='s3pXQ'><q id='s3pXQ'></q></dir></style></legend>
                            <tfoot id='s3pXQ'></tfoot>