由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - How to overload global new operator?
相关主题
我相信dart有戏FMP vs ExecutorService/Future
不是经常有人嚷嚷要contribute开源吗?Re: 大家介绍一下当下比较流行的网页编程吧 (转载)
go也是三种paradigm混合的语言那位大侠介绍一下python的webcrawler吧
问个土问题:Why iterator instead of array index for vector?有JAVA底子学习web service那些库或者framework有没有什么好方法
using Xercers c++ write XML in UTF-8 in SIJIS environmentnodejs到底有啥好的?大牛给解释解释?
class impl要不要用专业一点的语言来表达一下语言之争?
關於語言選擇重不重要有一点我不同意公孙大神说的
FMP mini profilelisp跟r比起来有什么优势?
相关话题的讨论汇总
话题: new话题: overload话题: operator话题: file话题: overloaded
进入Programming版参与讨论
1 (共1页)
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

相关主题
class implFMP vs ExecutorService/Future
關於語言選擇重不重要Re: 大家介绍一下当下比较流行的网页编程吧 (转载)
FMP mini profile那位大侠介绍一下python的webcrawler吧
进入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.

1 (共1页)
进入Programming版参与讨论
相关主题
lisp跟r比起来有什么优势?using Xercers c++ write XML in UTF-8 in SIJIS environment
做网站的语言,技术推荐 (转载)class impl
有人经历过前端后端,同工不同筹的那个年代吗?關於語言選擇重不重要
Pivotal Drops Groovy and GrailsFMP mini profile
我相信dart有戏FMP vs ExecutorService/Future
不是经常有人嚷嚷要contribute开源吗?Re: 大家介绍一下当下比较流行的网页编程吧 (转载)
go也是三种paradigm混合的语言那位大侠介绍一下python的webcrawler吧
问个土问题:Why iterator instead of array index for vector?有JAVA底子学习web service那些库或者framework有没有什么好方法
相关话题的讨论汇总
话题: new话题: overload话题: operator话题: file话题: overloaded