Calling builtin new

Discussion in 'C++' started by Shayne Wissler, Jan 30, 2004.

  1. 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, Jan 30, 2004
    #1
    1. Advertising

  2. 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 ::eek:perator new(size_t)

    .... my solution... if anyone has a better idea...
     
    =?ISO-8859-1?Q?Le_G=E9ant_Vert?=, Jan 30, 2004
    #2
    1. Advertising

  3. 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, Jan 30, 2004
    #3
  4. "Thomas Matthews" <> wrote in
    message news:RkASb.13798$...
    > 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
     
    Shayne Wissler, Jan 30, 2004
    #4
  5. Shayne Wissler

    Rolf Magnus Guest

    Shayne Wissler wrote:

    >
    > "Thomas Matthews" <> wrote
    > in message news:RkASb.13798$...
    >> 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.
     
    Rolf Magnus, Jan 30, 2004
    #5
  6. "Rolf Magnus" <> wrote in message
    news:bvemur$pqo$02$-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
     
    Shayne Wissler, Jan 30, 2004
    #6
  7. Shayne Wissler

    Rolf Magnus Guest

    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.
     
    Rolf Magnus, Jan 30, 2004
    #7
  8. Shayne Wissler

    lilburne Guest

    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.
     
    lilburne, Jan 30, 2004
    #8
  9. "Rolf Magnus" <> wrote in message
    news:bvepo6$vug$04$-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
     
    Shayne Wissler, Jan 31, 2004
    #9
  10. Shayne Wissler

    Rolf Magnus Guest

    Shayne Wissler wrote:

    >
    > "Rolf Magnus" <> wrote in message
    > news:bvepo6$vug$04$-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.
     
    Rolf Magnus, Jan 31, 2004
    #10
  11. Shayne Wissler

    Jerry Coffin Guest

    In article <DOySb.59476$U%5.345456@attbi_s03>,
    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.
     
    Jerry Coffin, Feb 14, 2004
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    6
    Views:
    453
    Michel de Becdelièvre
    Jun 11, 2005
  2. Caspy
    Replies:
    2
    Views:
    8,380
    Caspy
    Oct 14, 2005
  3. Asad
    Replies:
    1
    Views:
    4,452
    jmens
    Oct 19, 2006
  4. bdb112
    Replies:
    2
    Views:
    318
    Chris Torek
    Jul 2, 2011
  5. Replies:
    2
    Views:
    513
    Thomas 'PointedEars' Lahn
    Mar 11, 2008
Loading...

Share This Page