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

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

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

        C++ 析构函数的奇怪行为

        Weird behaviour of C++ destructors(C++ 析构函数的奇怪行为)

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

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

                  本文介绍了C++ 析构函数的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  #include <iostream>
                  #include <vector>
                  using namespace std;
                  
                  int main()
                  {
                      vector< vector<int> > dp(50000, vector<int>(4, -1));
                      cout << dp.size();
                  }
                  

                  这个小程序只需从命令行运行就需要几秒钟的时间来执行.但是当在调试器中运行时,它需要超过 8 秒.暂停调试器表明它正在销毁所有这些向量.跆拳道?

                  This tiny program takes a split second to execute when simply run from the command line. But when run in a debugger, it takes over 8 seconds. Pausing the debugger reveals that it is in the middle of destroying all those vectors. WTF?

                  注意 - Visual Studio 2008 SP1、Core 2 Duo 6700 CPU 和 2GB RAM.

                  Note - Visual Studio 2008 SP1, Core 2 Duo 6700 CPU with 2GB of RAM.

                  添加: 澄清一下,不,我没有混淆调试和发布版本.这些结果在同一个 .exe 上,中间甚至没有任何重新编译.事实上,在 Debug 和 Release 版本之间切换没有任何改变.

                  Added: To clarify, no, I'm not confusing Debug and Release builds. These results are on one and the same .exe, without even any recompiling inbetween. In fact, switching between Debug and Release builds changes nothing.

                  推荐答案

                  在调试器中运行将用于执行更多检查的内存分配库更改为一个.除了内存分配和解除分配什么都不做的程序将比正常"程序遭受更多的痛苦.

                  Running in the debugger changes the memory allocation library used to one that does a lot more checking. A program that does nothing but memory allocation and de-allocation is going to suffer much more than a "normal" program.

                  编辑刚刚尝试在 VS 下运行你的程序,我得到一个看起来像

                  Edit Having just tried running your program under VS I get a call stack that looks like

                  ntdll.dll!_RtlpValidateHeapEntry@12()  + 0x117 bytes    
                  ntdll.dll!_RtlDebugFreeHeap@12()  + 0x97 bytes  
                  ntdll.dll!_RtlFreeHeapSlowly@12()  + 0x228bf bytes  
                  ntdll.dll!_RtlFreeHeap@12()  + 0x17646 bytes    
                  msvcr90d.dll!_free_base(void * pBlock=0x0061f6e8)  Line 109 + 0x13 bytes
                  msvcr90d.dll!_free_dbg_nolock(void * pUserData=0x0061f708, int nBlockUse=1)
                  msvcr90d.dll!_free_dbg(void * pUserData=0x0061f708, int nBlockUse=1) 
                  msvcr90d.dll!operator delete(void * pUserData=0x0061f708)
                  desc.exe!std::allocator<int>::deallocate(int * _Ptr=0x0061f708, unsigned int __formal=4)
                  desc.exe!std::vector<int,std::allocator<int> >::_Tidy()  Line 1134  C++
                  

                  其中显示了 ntdll.dll 中的调试功能和正在使用的 C 运行时.

                  Which shows the debug functions in ntdll.dll and the C runtime being used.

                  这篇关于C++ 析构函数的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)
                  Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)
                  STL BigInt class implementation(STL BigInt 类实现)
                  Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)
                  Move list element to the end in STL(在 STL 中将列表元素移动到末尾)
                  Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)

                        <tbody id='WTn8A'></tbody>

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

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