分析C ++析构函数调用

时间:2014-03-25 20:09:12

标签: c++ profile

我正在使用来自intel composer xe 2013的intel c ++编译器分析在优化级别-O3上编译的C ++应用程序。分析器(OS X上的Instruments)指出,很大一部分时间用于调用析构函数特定类型的对象。但是,它不会向我提供有关首先分配对象的功能的信息。是否有任何工具可以提供有关哪些函数分配特定类型对象的最大数量的信息?

编辑:我还为intel c ++编译器尝试了-profile-functions标志但没有成功。

1 个答案:

答案 0 :(得分:1)

您可以向构造函数,文件和行号添加两个参数。将该信息保存在对象中,并在调用析构函数时将其打印出来。可选地,您可以隐藏构造函数的宏中的一些丑陋。

#include <iostream>
#include <string>
using std::string;

class Object
{
    string _file;
    int _line;
  public:
    Object( const char * file, int line ) : _file(file), _line(line) {}
   ~Object() { std::cerr << "dtor for object created in file: " << _file << " line: " << _line << std::endl; }
};

int main( int argc, char * argv[] )
{
    Object obj( __FILE__, __LINE__ );
    return 0;
}

这就是它的运行方式

$ g++ main.cpp -o main && ./main
dtor for object created in file: main.cpp line: 16
$
相关问题