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. Using Alternate Memory Heaps

Using Alternate Memory Heaps

Scheduled Pinned Locked Moved C / C++ / MFC
c++performancequestion
6 Posts 3 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.
  • Richard Andrew x64R Offline
    Richard Andrew x64R Offline
    Richard Andrew x64
    wrote on last edited by
    #1

    Is there a way to tell the C++ runtime which heap to use to allocate objects with "new"? Can it be changed on the fly, or must the program be restarted? I'd like to create custom heaps to hold objects, and then destroy the heaps when no longer needed, then create new heaps to take their places. I have some legacy classes that leak memory, and I'm hoping to be able to mitigate the leaks by creating the objects in separate heaps, and then destroying the heaps after X many hours of run time.

    The difficult we do right away... ...the impossible takes slightly longer.

    L D 2 Replies Last reply
    0
    • Richard Andrew x64R Richard Andrew x64

      Is there a way to tell the C++ runtime which heap to use to allocate objects with "new"? Can it be changed on the fly, or must the program be restarted? I'd like to create custom heaps to hold objects, and then destroy the heaps when no longer needed, then create new heaps to take their places. I have some legacy classes that leak memory, and I'm hoping to be able to mitigate the leaks by creating the objects in separate heaps, and then destroying the heaps after X many hours of run time.

      The difficult we do right away... ...the impossible takes slightly longer.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Take a look at Heap Functions | Microsoft Docs[^].

      1 Reply Last reply
      0
      • Richard Andrew x64R Richard Andrew x64

        Is there a way to tell the C++ runtime which heap to use to allocate objects with "new"? Can it be changed on the fly, or must the program be restarted? I'd like to create custom heaps to hold objects, and then destroy the heaps when no longer needed, then create new heaps to take their places. I have some legacy classes that leak memory, and I'm hoping to be able to mitigate the leaks by creating the objects in separate heaps, and then destroying the heaps after X many hours of run time.

        The difficult we do right away... ...the impossible takes slightly longer.

        D Offline
        D Offline
        Daniel Pfeffer
        wrote on last edited by
        #3

        The "canonical" way to do this is with class-specific new / delete:

        class Foo
        {
        public:
        void* operator new(size_t blockSize)
        {
        // allocate 'blockSize' and return it, or throw std::bad_alloc (defined in )
        }

        void operator delete(void\* p)
        {
            // release 'p'. Note that p == nullptr is allowed!
        }
        
        /\* rest of class \*/
        

        };

        As long as you use the same heap for both allocation and deletion, things should work fine. As Richard MacCutchan says, a quick-and-dirty way to do this in Windows is by using the Windows heap APIs.

        Freedom is the freedom to say that two plus two make four. If that is granted, all else follows. -- 6079 Smith W.

        Richard Andrew x64R 1 Reply Last reply
        0
        • D Daniel Pfeffer

          The "canonical" way to do this is with class-specific new / delete:

          class Foo
          {
          public:
          void* operator new(size_t blockSize)
          {
          // allocate 'blockSize' and return it, or throw std::bad_alloc (defined in )
          }

          void operator delete(void\* p)
          {
              // release 'p'. Note that p == nullptr is allowed!
          }
          
          /\* rest of class \*/
          

          };

          As long as you use the same heap for both allocation and deletion, things should work fine. As Richard MacCutchan says, a quick-and-dirty way to do this in Windows is by using the Windows heap APIs.

          Freedom is the freedom to say that two plus two make four. If that is granted, all else follows. -- 6079 Smith W.

          Richard Andrew x64R Offline
          Richard Andrew x64R Offline
          Richard Andrew x64
          wrote on last edited by
          #4

          Thank you Daniel. Does this solution mean that I must call the constructor myself?

          The difficult we do right away... ...the impossible takes slightly longer.

          D 1 Reply Last reply
          0
          • Richard Andrew x64R Richard Andrew x64

            Thank you Daniel. Does this solution mean that I must call the constructor myself?

            The difficult we do right away... ...the impossible takes slightly longer.

            D Offline
            D Offline
            Daniel Pfeffer
            wrote on last edited by
            #5

            No. When constructing an object using new (e.g. Foo* p = new Foo()), the compiler chooses the new operator to be called as follows: 1. A class-specific operator new() (if present) 2. The global operator new It is the same for the delete operator. You may also replace the global new and delete operators with your own code:

            // place outside any namespace!

            void* operator new(size_t blockSize)
            {
            // allocate 'blockSize' and return it, or throw std::bad_alloc (defined in )
            }

            void operator delete(void* p)
            {
            // release 'p'. Note that p == nullptr is allowed!
            }

            Note that operators new and delete deal only with memory allocation/release. The compiler still calls the class' constructor to turn the raw memory into a class instance, and the destructor in order to turn a class instance back into raw memory.

            Freedom is the freedom to say that two plus two make four. If that is granted, all else follows. -- 6079 Smith W.

            Richard Andrew x64R 1 Reply Last reply
            0
            • D Daniel Pfeffer

              No. When constructing an object using new (e.g. Foo* p = new Foo()), the compiler chooses the new operator to be called as follows: 1. A class-specific operator new() (if present) 2. The global operator new It is the same for the delete operator. You may also replace the global new and delete operators with your own code:

              // place outside any namespace!

              void* operator new(size_t blockSize)
              {
              // allocate 'blockSize' and return it, or throw std::bad_alloc (defined in )
              }

              void operator delete(void* p)
              {
              // release 'p'. Note that p == nullptr is allowed!
              }

              Note that operators new and delete deal only with memory allocation/release. The compiler still calls the class' constructor to turn the raw memory into a class instance, and the destructor in order to turn a class instance back into raw memory.

              Freedom is the freedom to say that two plus two make four. If that is granted, all else follows. -- 6079 Smith W.

              Richard Andrew x64R Offline
              Richard Andrew x64R Offline
              Richard Andrew x64
              wrote on last edited by
              #6

              Thank you. :)

              The difficult we do right away... ...the impossible takes slightly longer.

              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