首页 > 代码库 > C++中new的二三事

C++中new的二三事

 1 #include <iostream> 2 using std::cout; 3 using std::endl; 4 #define new New (__FILE__,  __LINE__)  5  6 void * operator new (size_t size,  const char * filename,  int lineno)  7 {  8  cout <<"NEW: filename =" <<filename <<",  lineno =" <<lineno << endl;  9  cout <<"Lineno? "<< size << endl;10  return operator new (size); 11 }12 int main (int argc,  char ** argv) 13 { 14  int * p = new  (__FILE__,  __LINE__) int ; 15  cout <<"Hello World!" <<endl; 16  delete p; 17  return 0; 18 } 

是的,一切都是因为在一篇老外的博客里面看到了第14行,因此我要挖个坑来埋自己。