Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 937451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:28:53+00:00 2026-05-15T21:28:53+00:00

Probably everyone ran into this problem at least once during development: while(/*some condition here

  • 0

Probably everyone ran into this problem at least once during development:

while(/*some condition here that somehow never will be false*/)
{
    ...
    yourvector.push_back(new SomeType());
    ...
}

As you see the program starts to drain all system memory, your program hangs and your system starts to swap like crazy. If you don’t recognize the problem fast enough and kill the process you probably get an unresponsive system in seconds where your mouse pointer don’t even moving. You can either wait your program crash with “out of memory” error (which may take several long minutes) or hit the reset on your computer.

If you can’t track down the bug immediately then you will need several tests and resets to find out which is very annoying…

I’m looking for a possibly cross-platform way to prevent this somehow. The best would be a debug mode code that exits the program if it allocated too much memory, but how can I keep track how much memory is allocated?
Overriding the global new and delete operators won’t help, because the free function I would invoke in the delete won’t give any idea how many bytes are freed.

Any ideas appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T21:28:54+00:00Added an answer on May 15, 2026 at 9:28 pm

    Overriding the global new and delete operators won’t help, because the free function I would invoke in the delete won’t give any idea how many bytes are freed.

    But you can make it so. Here’s a full framework for overloading the global memory operators (throw it in some global_memory.cpp file):

    namespace
    {   
        // utility
        std::new_handler get_new_handler(void)
        {
            std::new_handler handler = std::set_new_handler(0);
            std::set_new_handler(handler);
    
            return handler;
        }
    
        // custom allocation scheme goes here!
        void* allocate(std::size_t pAmount)
        {
    
        }
    
        void deallocate(void* pMemory)
        {
    
        }
    
        // allocate with throw, properly
        void* allocate_throw(std::size_t pAmount)
        {
            void* result = allocate(pAmount);
    
            while (!result)
            {
                // call failure handler
                std::new_handler handler = get_new_handler();
                if (!handler)
                {
                    throw std::bad_alloc();
                }
    
                handler();
    
                // try again
                result = allocate(pAmount);
            }
    
            return result;
        }
    }
    
    void* operator new(std::size_t pAmount) throw(std::bad_alloc)
    {
        return allocate_throw(pAmount);
    }
    
    void *operator new[](std::size_t pAmount) throw(std::bad_alloc)
    {
        return allocate_throw(pAmount);
    }
    
    void *operator new(std::size_t pAmount, const std::nothrow_t&) throw()
    {
        return allocate(pAmount);
    }
    
    void *operator new[](std::size_t pAmount, const std::nothrow_t&) throw()
    {
        return allocate(pAmount);
    }
    
    void operator delete(void* pMemory) throw()
    {
        deallocate(pMemory);
    }
    
    void operator delete[](void* pMemory) throw()
    {
        deallocate(pMemory);
    }
    
    void operator delete(void* pMemory, const std::nothrow_t&) throw()
    {
        deallocate(pMemory);
    }
    
    void operator delete[](void* pMemory, const std::nothrow_t&) throw()
    {
        deallocate(pMemory);
    }
    

    Then you can do something like:

        // custom allocation scheme goes here!
        const std::size_t allocation_limit = 1073741824; // 1G
        std::size_t totalAllocation = 0;
    
        void* allocate(std::size_t pAmount)
        {
            // make sure we're within bounds
            assert(totalAllocation + pAmount < allocation_limit);
    
            // over allocate to store size
            void* mem = std::malloc(pAmount + sizeof(std::size_t));
            if (!mem)
                return 0;
    
            // track amount, return remainder
            totalAllocation += pAmount;
            *static_cast<std::size_t*>(mem) = pAmount;
    
            return static_cast<char*>(mem) + sizeof(std::size_t);
        }
    
        void deallocate(void* pMemory)
        {
            // get original block
            void* mem = static_cast<char*>(pMemory) - sizeof(std::size_t);
    
            // track amount
            std::size_t amount = *static_cast<std::size_t*>(mem);
            totalAllocation -= pAmount;
    
            // free
            std::free(mem);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Probably a very trivial problem. I have an object that looks like this: @PersistenceCapable
Hi everyone this is probably something extremely simple that i'm overlooking but can someone
I suppose this is partially subjective in that it's probably dependent on everyone's interpretation
MOV is probably the first instruction everyone learns while learning ASM. Just now I
Probably something simple, but heres my problem, I have implememnted a simple captcha into
Everyone who's done any web application development in Scala knows that you can use
By now everyone writing for Windows probably knows that applications cannot (officially) steal focus
I suppose everyone here already has seen one of these (or at least a
I'm probably over-thinking this, but I've been stuck on it a while, so I
I have a problem with the standard JSpinner.DateEditor (as probably does everyone else). When

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.