打电话给新建 [英] Calling builtin new

查看:77
本文介绍了打电话给新建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经超载了全局的新操作员,所以我可以检测到我已经运行了什么时候内存:
extern:


extern void * operator new(size_t s){

void * r = malloc(s);

if(!r&& s){

fprintf(stderr,错误:没有更多记忆。);

退出(1);

}

返回r;

}


这里的问题是我真的不想打电话给malloc(和 - Valgrind

对我这么做很吵);我想打电话给我实施

new over overrode。建议?

谢谢,

Shayne Wissler

解决方案

Shayne Wissler写道:

我已经超载了全局的新操作符,以便我可以检测到我的内存运行时间是否已停止:

extern void * operator new(size_t s){
void * r = malloc(s);
if(!r&& s){
fprintf(stderr,"错误:没有更多的记忆。");
退出(1);
}
返回r;
}

这里的问题是我不是真的想打电话给malloc(和 - Valgrind对我这么做很吵);我想调用我覆盖的新的实现。建议?

谢谢,
Shayne Wissler



我做了完全相同的事情来检测由于不匹配导致的内存泄漏

new / delete;我做了什么重载operator new以及更多

参数:大小,源文件名和行(显示内存泄漏的地方显示) 。

如果你这样做了(显示

例子中出现内存不足的地方),你会有类似的事情: br />

void * operator new(size_t,char *,unsigned long);


你调用原文时没有任何问题: :operator new(size_t)


....我的解决方案......如果有人有更好的主意......


Shayne Wissler写道:

我已经超载了全局新操作符,因此我可以检测到我运行时内存不足:


我的理解是新当存在内存问题时,运算符抛出异常

。也许你可以抓住

异常来检测内存不足的时间。


extern void * operator new(size_t s){
void * r = malloc(s);
if(!r&& s){
fprintf(stderr,Error:No more memory。);
退出(1);
}
返回r;
}

这里的问题是我真的不想打电话给malloc(和 - Valgrind
关于我这样做很吵);我想调用我覆盖的新的实现。建议?


这里我不明白。如果你重载全局new运算符,那么你的函数会调用全局new运算符,这不是这个吗?b $ b叫做递归(或者是一个无限循环)? br />

谢谢,
Shayne Wissler



-

Thomas Matthews

C ++新闻组欢迎辞:
http://www.slack.net/~shiva/welcome.txt

C ++常见问题: http://www.parashift.com/c++-faq-lite

C常见问题: http://www.eskimo.com/~scs/c-faq/top。 html

alt.comp.lang.learn.c-c ++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html

其他网站:
http://www.josuttis.com - C ++ STL图书馆书籍


< blockquote>

" Thomas Matthews" <钍**************************** @ sbcglobal.net>写在

消息新闻:Rk ****************** @ newssvr16.news.prodigy.c om ...

Shayne Wissler写道:

我已经重载了全局的新运算符,这样我就可以检测到我的
内存不足了:



我的理解是新当存在内存问题时,运算符会抛出异常。也许你可以抓住
异常来检测内存不足。




确实如此,谢谢。但是,当我测试

这个时,我抓住了catch(...),是否有一个标准的例外我应该抓住而不是这个

catch-all?

这里的问题是我真的不想打电话给malloc
(并且 - 对于我这样做,Valgrind非常吵) ;我想调用我覆盖的新实现
。建议?



这里我不明白。如果重载全局new运算符,那么让你的函数调用全局new运算符,这不是叫递归(或者是无限循环)吗?




我的意思是我要打电话给内置的新的,我用的是
覆盖的那个。就像我覆盖虚拟方法一样,我仍然可以调用该方法的

基类版本。我如何引用基础?

新版本的运营商?

Shayne Wissler


I''ve overloaded the global new operator, so that I can, detect when I''ve run
out of memory:

extern void* operator new(size_t s) {
void *r = malloc(s);
if (!r && s) {
fprintf(stderr, "Error: No more memory.");
exit(1);
}
return r;
}

The problem here is that I don''t really want to call malloc (and--Valgrind
is quite noisy about me doing this); I want to call the implementation of
new that I overrode. Suggestions?
Thanks,
Shayne Wissler

解决方案

Shayne Wissler wrote:

I''ve overloaded the global new operator, so that I can, detect when I''ve run
out of memory:

extern void* operator new(size_t s) {
void *r = malloc(s);
if (!r && s) {
fprintf(stderr, "Error: No more memory.");
exit(1);
}
return r;
}

The problem here is that I don''t really want to call malloc (and--Valgrind
is quite noisy about me doing this); I want to call the implementation of
new that I overrode. Suggestions?
Thanks,
Shayne Wissler


I''ve done quite the same thing to detect memory leaks due to mismatching
new/delete ; what I''ve done overload operator new with some more
parameters : the size, the filename of the source, and the line (to
display where memory leak occurs).
if you do as I''ve done (to display where "out of memory" occurs for
example), you''ll have something like :

void *operator new(size_t, char *, unsigned long);

and you won''t have any problem calling the original ::operator new(size_t)

.... my solution... if anyone has a better idea...


Shayne Wissler wrote:

I''ve overloaded the global new operator, so that I can, detect when I''ve run
out of memory:
My understanding is that the "new" operator throws an exception
when there are memory problems. Perhaps you could catch the
exception to detect when you''ve run out of memory.

extern void* operator new(size_t s) {
void *r = malloc(s);
if (!r && s) {
fprintf(stderr, "Error: No more memory.");
exit(1);
}
return r;
}

The problem here is that I don''t really want to call malloc (and--Valgrind
is quite noisy about me doing this); I want to call the implementation of
new that I overrode. Suggestions?
Here I don''t understand. If you overload the global new operator,
then have your function call the global new operator, isn''t this
called recursion (or perhaps an infinite loop)?


Thanks,
Shayne Wissler


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book



"Thomas Matthews" <Th****************************@sbcglobal.net> wrote in
message news:Rk******************@newssvr16.news.prodigy.c om...

Shayne Wissler wrote:

I''ve overloaded the global new operator, so that I can, detect when I''ve run out of memory:



My understanding is that the "new" operator throws an exception
when there are memory problems. Perhaps you could catch the
exception to detect when you''ve run out of memory.



It does indeed, thanks. However, I caught with catch(...) when I tested
this, is there a standard exception I should be catching instead of this
catch-all?

The problem here is that I don''t really want to call malloc (and--Valgrind is quite noisy about me doing this); I want to call the implementation of new that I overrode. Suggestions?



Here I don''t understand. If you overload the global new operator,
then have your function call the global new operator, isn''t this
called recursion (or perhaps an infinite loop)?



What I meant was that I want to call the builtin-new, the one I had
overridden. Just like when I override a virtual method, I can still call the
base class''s version of that method. How do I refer to the "base" version of
the new operator?
Shayne Wissler


这篇关于打电话给新建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆