Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. overloading new operator

overloading new operator

Scheduled Pinned Locked Moved C / C++ / MFC
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    ragavan
    wrote on last edited by
    #1

    Hi Can anyone tell me why it is necessary to overload new and delete operator Thanks in advance

    C T J 3 Replies Last reply
    0
    • R ragavan

      Hi Can anyone tell me why it is necessary to overload new and delete operator Thanks in advance

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      ragavan wrote:

      Can anyone tell me why it is necessary to overload new and delete operator

      It is certainly not necessary to overload these operators. It can be done, sure, but it is never needed. I never did that myself (and I don't see a reason why I would do so). An example of what you can do by overloading these operators is what MFC does: it allows to track memory leaks.


      Cédric Moonen Software developer
      Charting control [v1.2]

      H J 2 Replies Last reply
      0
      • C Cedric Moonen

        ragavan wrote:

        Can anyone tell me why it is necessary to overload new and delete operator

        It is certainly not necessary to overload these operators. It can be done, sure, but it is never needed. I never did that myself (and I don't see a reason why I would do so). An example of what you can do by overloading these operators is what MFC does: it allows to track memory leaks.


        Cédric Moonen Software developer
        Charting control [v1.2]

        H Offline
        H Offline
        Hans Dietrich
        wrote on last edited by
        #3

        Well put. It seems like the OP meant to say "when it is necessary" rather than "why it is necessary".

        1 Reply Last reply
        0
        • R ragavan

          Hi Can anyone tell me why it is necessary to overload new and delete operator Thanks in advance

          T Offline
          T Offline
          toxcct
          wrote on last edited by
          #4

          ragavan wrote:

          why it is necessary to overload new and delete operator

          to put some extra printf() inside them for instance !? ;)


          [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

          1 Reply Last reply
          0
          • R ragavan

            Hi Can anyone tell me why it is necessary to overload new and delete operator Thanks in advance

            J Offline
            J Offline
            James R Twine
            wrote on last edited by
            #5

            One (the only?) reason is when the objects need to be allocated in a non-standard way.  For example, for performance reasons, you may be using per-thread heaps so as to avoid heap contention in a multi-threaded application (if the application is allocation intensive).    Or if you have to allocate lots of smaller-sized objects (< 16-32 bytes) and you need to optimize the allocations by allocating larger blocks and then chunking them yourself.  Combining the Win32 heap's and C-RTL's overhead, a heap allocation can have between 20-32 bytes of overhead.  So if you need to allocate one million objects of 8 bytes each, this overhead (both space and execution time) can start to add up quickly.    Granted, the average everyday developer does not need to be (or simply is not) concerned with such things, but those are some reasons I have done it in the past.    Peace!

            -=- James
            Please rate this message - let me know if I helped or not! * * * If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
            Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
            See DeleteFXPFiles

            1 Reply Last reply
            0
            • C Cedric Moonen

              ragavan wrote:

              Can anyone tell me why it is necessary to overload new and delete operator

              It is certainly not necessary to overload these operators. It can be done, sure, but it is never needed. I never did that myself (and I don't see a reason why I would do so). An example of what you can do by overloading these operators is what MFC does: it allows to track memory leaks.


              Cédric Moonen Software developer
              Charting control [v1.2]

              J Offline
              J Offline
              James R Twine
              wrote on last edited by
              #6

              Cedric Moonen wrote:

              It is certainly not necessary to overload these operators. It can be done, sure, but it is never needed. I never did that myself (and I don't see a reason why I would do so).

              Yikes!  Please be careful saying things like this to less-experienced developers - just because you never had a reason to do so does not mean that one does not exist. :P

              Cedric Moonen wrote:

              An example of what you can do by overloading these operators is what MFC does: it allows to track memory leaks.

              Unless MFC changed recently, I believe this was mostly done by the use of DEBUG_NEW instead of new, and this in turn happened thanks to a #define that was in effect for debug builds.  That is not really overloading new...    Peace!

              -=- James
              Please rate this message - let me know if I helped or not! * * * If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
              Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
              See DeleteFXPFiles

              C 1 Reply Last reply
              0
              • J James R Twine

                Cedric Moonen wrote:

                It is certainly not necessary to overload these operators. It can be done, sure, but it is never needed. I never did that myself (and I don't see a reason why I would do so).

                Yikes!  Please be careful saying things like this to less-experienced developers - just because you never had a reason to do so does not mean that one does not exist. :P

                Cedric Moonen wrote:

                An example of what you can do by overloading these operators is what MFC does: it allows to track memory leaks.

                Unless MFC changed recently, I believe this was mostly done by the use of DEBUG_NEW instead of new, and this in turn happened thanks to a #define that was in effect for debug builds.  That is not really overloading new...    Peace!

                -=- James
                Please rate this message - let me know if I helped or not! * * * If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
                Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
                See DeleteFXPFiles

                C Offline
                C Offline
                Cedric Moonen
                wrote on last edited by
                #7

                James R. Twine wrote:

                Yikes! Please be careful saying things like this to less-experienced developers - just because you never had a reason to do so does not mean that one does not exist.

                That was certainly not my intention. Maybe I expressed myself badly.


                Cédric Moonen Software developer
                Charting control [v1.2]

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups