Calling builtin new

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Shayne Wissler

    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


  • Le Géant Vert

    #2
    Re: Calling builtin new

    Shayne Wissler wrote:
    [color=blue]
    >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
    >
    >
    >
    >[/color]
    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...

    Comment

    • Thomas Matthews

      #3
      Re: Calling builtin new

      Shayne Wissler wrote:[color=blue]
      > I've overloaded the global new operator, so that I can, detect when I've run
      > out of memory:[/color]

      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.

      [color=blue]
      > 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?[/color]

      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)?
      [color=blue]
      >
      >
      > Thanks,
      > Shayne Wissler
      >
      >[/color]


      --
      Thomas Matthews

      C++ newsgroup welcome message:

      C++ Faq: http://www.parashift.com/c++-faq-lite
      C Faq: http://www.eskimo.com/~scs/c-faq/top.html
      alt.comp.lang.l earn.c-c++ faq:

      Other sites:
      http://www.josuttis.com -- C++ STL Library book

      Comment

      • Shayne Wissler

        #4
        Re: Calling builtin new


        "Thomas Matthews" <Thomas_Matthew sSpitsOnSpamBot s@sbcglobal.net > wrote in
        message news:RkASb.1379 8$fH5.5665@news svr16.news.prod igy.com...[color=blue]
        > Shayne Wissler wrote:[color=green]
        > > I've overloaded the global new operator, so that I can, detect when I've[/color][/color]
        run[color=blue][color=green]
        > > out of memory:[/color]
        >
        > 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.[/color]

        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?
        [color=blue][color=green]
        > > The problem here is that I don't really want to call malloc[/color][/color]
        (and--Valgrind[color=blue][color=green]
        > > is quite noisy about me doing this); I want to call the implementation[/color][/color]
        of[color=blue][color=green]
        > > new that I overrode. Suggestions?[/color]
        >
        > 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)?[/color]

        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


        Comment

        • Rolf Magnus

          #5
          Re: Calling builtin new

          Shayne Wissler wrote:
          [color=blue]
          >
          > "Thomas Matthews" <Thomas_Matthew sSpitsOnSpamBot s@sbcglobal.net > wrote
          > in message news:RkASb.1379 8$fH5.5665@news svr16.news.prod igy.com...[color=green]
          >> Shayne Wissler wrote:[color=darkred]
          >> > I've overloaded the global new operator, so that I can, detect when
          >> > I've[/color][/color]
          > run[color=green][color=darkred]
          >> > out of memory:[/color]
          >>
          >> 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.[/color]
          >
          > 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?[/color]

          yes, std::bad_alloc.
          [color=blue][color=green][color=darkred]
          >> > The problem here is that I don't really want to call malloc[/color][/color]
          > (and--Valgrind[color=green][color=darkred]
          >> > is quite noisy about me doing this); I want to call the
          >> > implementation[/color][/color]
          > of[color=green][color=darkred]
          >> > new that I overrode. Suggestions?[/color]
          >>
          >> 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)?[/color]
          >
          > 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.[/color]

          That's a member of another class though.
          [color=blue]
          > How do I refer to the "base" version of the new operator?[/color]

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


          Comment

          • Shayne Wissler

            #6
            Re: Calling builtin new


            "Rolf Magnus" <ramagnus@t-online.de> wrote in message
            news:bvemur$pqo $02$1@news.t-online.com...
            [color=blue][color=green]
            > > 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?[/color]
            >
            > yes, std::bad_alloc.[/color]

            Thanks.
            [color=blue][color=green][color=darkred]
            > >> 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)?[/color]
            > >
            > > 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.[/color]
            >
            > That's a member of another class though.[/color]

            The principle is the same.
            [color=blue][color=green]
            > > How do I refer to the "base" version of the new operator?[/color]
            >
            > I don't know if that's possible at all.[/color]

            One ought to be able to do such a thing.


            Shayne Wissler


            Comment

            • lilburne

              #7
              Re: Calling builtin new

              Thomas Matthews wrote:
              [color=blue]
              > Shayne Wissler wrote:
              >[color=green]
              >> I've overloaded the global new operator, so that I can, detect when
              >> I've run
              >> out of memory:[/color]
              >
              >
              > 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.
              >[/color]

              Why not simply do a:

              void myFunctionToCal lWhenOutOfMemor y();

              set_new_handler (myFunctionToCa llWhenOutOfMemo ry);

              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.

              Comment

              • Rolf Magnus

                #8
                Re: Calling builtin new

                Shayne Wissler wrote:
                [color=blue][color=green][color=darkred]
                >> >> 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.[/color]
                >>
                >> That's a member of another class though.[/color]
                >
                > The principle is the same.[/color]

                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.
                [color=blue][color=green][color=darkred]
                >> > How do I refer to the "base" version of the new operator?[/color]
                >>
                >> I don't know if that's possible at all.[/color]
                >
                > One ought to be able to do such a thing.[/color]

                As I said, I don't know.

                Comment

                • Shayne Wissler

                  #9
                  Re: Calling builtin new


                  "Rolf Magnus" <ramagnus@t-online.de> wrote in message
                  news:bvepo6$vug $04$1@news.t-online.com...[color=blue]
                  > Shayne Wissler wrote:
                  >[color=green][color=darkred]
                  > >> >> 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.[/color]
                  > >
                  > > The principle is the same.[/color]
                  >
                  > 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.[/color]

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


                  Shayne Wissler


                  Comment

                  • Rolf Magnus

                    #10
                    Re: Calling builtin new

                    Shayne Wissler wrote:
                    [color=blue]
                    >
                    > "Rolf Magnus" <ramagnus@t-online.de> wrote in message
                    > news:bvepo6$vug $04$1@news.t-online.com...[color=green]
                    >> Shayne Wissler wrote:
                    >>[color=darkred]
                    >> >> >> 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.[/color]
                    >>
                    >> 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.[/color]
                    >
                    > I said the principle was the same, not the scenario.[/color]

                    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.

                    Comment

                    • Jerry Coffin

                      #11
                      Re: Calling builtin new

                      In article <DOySb.59476$U% 5.345456@attbi_ s03>, thalesNOSPAM000 @yahoo.com
                      says...[color=blue]
                      > I've overloaded the global new operator, so that I can, detect when I've run
                      > out of memory:[/color]

                      There's no need to overload operator new to handle this -- the library
                      provides set_new_handler for precisely this purpose.
                      [color=blue]
                      > 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?[/color]

                      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.

                      Comment

                      Working...