472,986 Members | 3,108 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,986 software developers and data experts.

Calling builtin new

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
Jul 22 '05 #1
10 1822
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...
Jul 22 '05 #2
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

Jul 22 '05 #3

"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
Jul 22 '05 #4
Shayne Wissler wrote:

"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?


yes, std::bad_alloc.
> 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.


That's a member of another class though.
How do I refer to the "base" version of the new operator?


I don't know if that's possible at all.
Jul 22 '05 #5

"Rolf Magnus" <ra******@t-online.de> wrote in message
news:bv*************@news.t-online.com...
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?


yes, std::bad_alloc.


Thanks.
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.


That's a member of another class though.


The principle is the same.
How do I refer to the "base" version of the new operator?


I don't know if that's possible at all.


One ought to be able to do such a thing.
Shayne Wissler
Jul 22 '05 #6
Thomas Matthews wrote:
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.


Why not simply do a:

void myFunctionToCallWhenOutOfMemory();

set_new_handler(myFunctionToCallWhenOutOfMemory);

Section 18.4.2.2 and 18.4.2.3. Then you get to find out when
memory is exhausted, before exceptions are thrown, and have
the opportunity to free up some memory (page out to
secondary storage). If a handler is set two attempts are
made to obtain memory the exception is only thrown if the
second attempt fails.

Jul 22 '05 #7
Shayne Wissler wrote:
>> 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.


That's a member of another class though.


The principle is the same.


Not really. You want to call a built-in function from a user-defined
function that replaces it, which is very different from the overloading
example, where you just call a user-defined fuction from another
user-defined function.
Other example: If you implement your own operator=(), how would you call
the builtin operator=() for the same class from that? I'd assume there
is none in this case (after all - you replaced it), so it can't be
called.
> How do I refer to the "base" version of the new operator?


I don't know if that's possible at all.


One ought to be able to do such a thing.


As I said, I don't know.

Jul 22 '05 #8

"Rolf Magnus" <ra******@t-online.de> wrote in message
news:bv*************@news.t-online.com...
Shayne Wissler wrote:
>> 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.

That's a member of another class though.


The principle is the same.


Not really. You want to call a built-in function from a user-defined
function that replaces it, which is very different from the overloading
example, where you just call a user-defined fuction from another
user-defined function.


I said the principle was the same, not the scenario.
Shayne Wissler
Jul 22 '05 #9
Shayne Wissler wrote:

"Rolf Magnus" <ra******@t-online.de> wrote in message
news:bv*************@news.t-online.com...
Shayne Wissler wrote:
>> >> 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.
>>
>> That's a member of another class though.
>
> The principle is the same.


Not really. You want to call a built-in function from a user-defined
function that replaces it, which is very different from the
overloading example, where you just call a user-defined fuction from
another user-defined function.


I said the principle was the same, not the scenario.


And my point is that the principle is not at all the same. One is simple
calling a regular function, the other one is calling a built-in that
was replaced.

Jul 22 '05 #10
In article <DOySb.59476$U%5.345456@attbi_s03>, th*************@yahoo.com
says...
I've overloaded the global new operator, so that I can, detect when I've run
out of memory:
There's no need to overload operator new to handle this -- the library
provides set_new_handler for precisely this purpose.
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?


Yes: don't even try this -- it's doomed from the beginning.

From the viewpoint of the compiler, you've overridden a function which
is fine and well. From the viewpoint of the linker (at least on the
typical implementations that use linkers) this operator satisfies all
external references in the program to the symbol with whatever name
global operator new mangles to with your compiler. Since the references
to the name have been satisfied, the linker will NOT include the one
that's in the standard library.

IOW, you can't call it because in your program it doesn't even exist.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 22 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Blair Hall | last post by:
Can anyone please tell me how to correctly use a built in function when there is a function of the same name in local scope? Here is an example. Suppose the following is in myApply.py: def...
0
by: Joachim Dahl | last post by:
I would like to inherit the builtin array class and write som extension methods in C. Other people have suggested that I use numarray for that, but all I want is a simple continuous C array of...
2
by: BJörn Lindqvist | last post by:
A problem I have occured recently is that I want to subclass builtin types. Especially subclassing list is very troublesome to me. But I can't find the right syntax to use. Take for example this...
1
by: Stephen Ferg | last post by:
Python has a builtin class for staticmethod. Seems to me that Python should also have a builtin class for abstractmethod. Something like this... ####################################### #...
11
by: rmm | last post by:
If I replace the open builtin eg import main __main__.__builtins__.open=None Is there any way, from here on, to access the original open function?? Extending this slightly, lets say I put a...
2
by: Casey Hawthorne | last post by:
Is there a way to determine -- when parsing -- if a word contains a builtin name or other imported system module name? Like "iskeyword" determines if a word is a keyword! -- Regards, Casey
1
by: barnesc | last post by:
Hi again, Since my linear algebra library appears not to serve any practical need (I found cgkit, and that works better for me), I've gotten bored and went back to one of my other projects:...
6
by: Anders K. Olsen | last post by:
Hello group I'm trying to list the users and groups who has read access to a file. I use .NET 2.0 and FileInfo.GetAccessControl().GetAccessRules(...) and then loop through the...
0
by: nejucomo | last post by:
Hi folks, Quick Synopsis: A test script demonstrates a memory leak when I use pythonic extensions of my builtin types, but if I use the builtin types themselves there is no memory leak. ...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.