j****i 发帖数: 305 | 1 I have a large project with many source codes in c++. I have access to the
source.
How can I overload the global operator new? If I define "new" in a header,
how do I make
sure every class in the project will use that?
Currently I overloaded new in the cpp file that has main(), but it only has
effect on
the new operators in that file.
Any ideas? |
N***m 发帖数: 4460 | 2 窃窃地同问,能不能用namespace?
抑或自定义成new(size_t,another_parameter)?
has
【在 j****i 的大作中提到】 : I have a large project with many source codes in c++. I have access to the : source. : How can I overload the global operator new? If I define "new" in a header, : how do I make : sure every class in the project will use that? : Currently I overloaded new in the cpp file that has main(), but it only has : effect on : the new operators in that file. : Any ideas?
|
j****i 发帖数: 305 | 3 You can redefine new to have other arguments, but how to make sure all the
files in the library use your version.
【在 N***m 的大作中提到】 : 窃窃地同问,能不能用namespace? : 抑或自定义成new(size_t,another_parameter)? : : has
|
N***m 发帖数: 4460 | 4 brutal forcely use regex to replace them all. |
X****r 发帖数: 3557 | 5 Your overloaded 'new' must be declared in every translation unit
of your program. In other words, every .cpp file need to include
the header which has the declaration directly or indirectly.
This is just like any other function you overload -- if the compiler
doesn't see your declaration, it won't know to use your overloaded
function.
the
header,
has
【在 j****i 的大作中提到】 : I have a large project with many source codes in c++. I have access to the : source. : How can I overload the global operator new? If I define "new" in a header, : how do I make : sure every class in the project will use that? : Currently I overloaded new in the cpp file that has main(), but it only has : effect on : the new operators in that file. : Any ideas?
|
N***m 发帖数: 4460 | 6 那global的new为啥这么特殊呢?咋不包括也能用呢?
【在 X****r 的大作中提到】 : Your overloaded 'new' must be declared in every translation unit : of your program. In other words, every .cpp file need to include : the header which has the declaration directly or indirectly. : This is just like any other function you overload -- if the compiler : doesn't see your declaration, it won't know to use your overloaded : function. : : the : header, : has
|
X****r 发帖数: 3557 | 7 这个和其他的+-*/一样,系统给你预定义好的啊。
【在 N***m 的大作中提到】 : 那global的new为啥这么特殊呢?咋不包括也能用呢?
|
j****i 发帖数: 305 | 8 I want to overload operator new and delete to detect memory leaks,
according to this article: http://www.flipcode.com/archives/How_To_Find_Memory_Leaks.shtml
He could do it by putting the new def of new stdafx.h, but I'm programming
in unix environment,
how can achieve the same thing without changing all my source codes?
【在 X****r 的大作中提到】 : Your overloaded 'new' must be declared in every translation unit : of your program. In other words, every .cpp file need to include : the header which has the declaration directly or indirectly. : This is just like any other function you overload -- if the compiler : doesn't see your declaration, it won't know to use your overloaded : function. : : the : header, : has
|
X****r 发帖数: 3557 | 9 Have your considered using an existing tool to detect memory leaks,
e.g. valgrind?
http://www.flipcode.com/archives/How_To_Find_Memory_Leaks.shtml
programming
【在 j****i 的大作中提到】 : I want to overload operator new and delete to detect memory leaks, : according to this article: http://www.flipcode.com/archives/How_To_Find_Memory_Leaks.shtml : He could do it by putting the new def of new stdafx.h, but I'm programming : in unix environment, : how can achieve the same thing without changing all my source codes?
|
j****i 发帖数: 305 | 10 Used valgrind several times before, my impression is that it is very slow.
Just came across this technique, and want to try it out.
【在 X****r 的大作中提到】 : Have your considered using an existing tool to detect memory leaks, : e.g. valgrind? : : http://www.flipcode.com/archives/How_To_Find_Memory_Leaks.shtml : programming
|
|
|
X****r 发帖数: 3557 | 11 gcc option "-include" allows you to include file from the command
line, which might be easier than modifying all the sources.
slow.
【在 j****i 的大作中提到】 : Used valgrind several times before, my impression is that it is very slow. : Just came across this technique, and want to try it out.
|
j****i 发帖数: 305 | 12 Great idea!
But when I do that, boost::shared_count.hpp gives me problem when I compile.
Here's the line in boost that doesn't compile:
new( static_cast< void* >( pi_ ) ) impl_type( p, d, a );
the error message is:
/usr/local/include/boost/smart_ptr/detail/shared_count.hpp:160: error:
expected type-specifier before 'static_cast'
Seems that the placement new does not like the overloaded new.
With the following overloaded new:
void * operator new(size_t size,
【在 X****r 的大作中提到】 : gcc option "-include" allows you to include file from the command : line, which might be easier than modifying all the sources. : : slow.
|
j****i 发帖数: 305 | 13 Forgot about one thing, here's the macro:
#ifdef _DEBUG
#define DEBUG_NEW new(__FILE__, __LINE__)
#else
#define DEBUG_NEW new
#endif
#define new DEBUG_NEW
compile.
【在 j****i 的大作中提到】 : Great idea! : But when I do that, boost::shared_count.hpp gives me problem when I compile. : Here's the line in boost that doesn't compile: : new( static_cast< void* >( pi_ ) ) impl_type( p, d, a ); : the error message is: : /usr/local/include/boost/smart_ptr/detail/shared_count.hpp:160: error: : expected type-specifier before 'static_cast' : Seems that the placement new does not like the overloaded new. : With the following overloaded new: : void * operator new(size_t size,
|
N***m 发帖数: 4460 | 14 哦,我还以为new是藏在什么特殊地方的呢。
【在 X****r 的大作中提到】 : 这个和其他的+-*/一样,系统给你预定义好的啊。
|
N***m 发帖数: 4460 | 15 这个预定义看起来头好晕阿
【在 j****i 的大作中提到】 : Forgot about one thing, here's the macro: : #ifdef _DEBUG : #define DEBUG_NEW new(__FILE__, __LINE__) : #else : #define DEBUG_NEW new : #endif : #define new DEBUG_NEW : : compile.
|
p***o 发帖数: 1252 | 16 Wow! The one from the stone-age MFC. I don't think it could
support placement new. A much better idea is to store the
whole call stack at the point of new/delete (though I don't
know how to get the call stack). Then you don't need this
macro that break most modern C++ libraries.
【在 j****i 的大作中提到】 : Forgot about one thing, here's the macro: : #ifdef _DEBUG : #define DEBUG_NEW new(__FILE__, __LINE__) : #else : #define DEBUG_NEW new : #endif : #define new DEBUG_NEW : : compile.
|
d****p 发帖数: 685 | 17 I guess he didn't provide a placement new implementation in his custom new
stuff.
【在 p***o 的大作中提到】 : Wow! The one from the stone-age MFC. I don't think it could : support placement new. A much better idea is to store the : whole call stack at the point of new/delete (though I don't : know how to get the call stack). Then you don't need this : macro that break most modern C++ libraries.
|
p***o 发帖数: 1252 | 18 No, the #define blindly replace all new with new(__FILE__, __LINE__),
so placement new (p) become new(__FILE__, __LINE__) (p), which is
a syntax error ...
【在 d****p 的大作中提到】 : I guess he didn't provide a placement new implementation in his custom new : stuff.
|
j****i 发帖数: 305 | 19 More details please. These macros did screw up placement new.
【在 p***o 的大作中提到】 : Wow! The one from the stone-age MFC. I don't think it could : support placement new. A much better idea is to store the : whole call stack at the point of new/delete (though I don't : know how to get the call stack). Then you don't need this : macro that break most modern C++ libraries.
|