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

  • SEARCH
  • Home
  • 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 6709853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:55:53+00:00 2026-05-26T07:55:53+00:00

i had asked help on this question here Static member reclaiming memory and recovering

  • 0

i had asked help on this question here Static member reclaiming memory and recovering from an exception

the program below is to allocate memory using own new operator. I have to throw exception on 5th object allocation and recover by freeing up memory (strange question i know, but it is assignment)

i have written the code here. allocation works, but when i try to call delete (through option ‘2’) i get stuck in infinite loop.

#include <iostream>
#include <cstdlib>

using namespace std;

class object
{
    int data;
    static int count;
    static void* allocatedObjects[5];
public:
    object() {  }
    void* operator new(size_t);
    void operator delete(void *);
    void static release();
    void static printCount()
    {
        cout << count << '\n';
    }
    ~object()
    {
        release();
    }
};

int object::count = 0;
void* object::allocatedObjects[5];

void* object::operator new(size_t size)
{
    if (count > 5)
        throw "Cannot allocate more than 5 objects!\n";
    void *p = malloc(size);
    allocatedObjects[count] = p;
    count++;
    return p;
}

void object::operator delete(void *p)
{
    free(p);
    count--;
}

void object::release()
{
    while (count > 0)
    {
        delete static_cast<object*> (allocatedObjects[count]);
    }
}

int main()
{
    object *o[10];
    int i = -1;
    char c = 1;
    while (c != '3')
    {
        cout << "Number of objects currently allocated : ";
        object::printCount();
        cout << "1. Allocate memory for object.\n";
        cout << "2. Deallocate memory of last object.\n";
        cout << "3. Exit.\n";
        cin >> c;
        if (c == '1')
        {
            try
            {
                i++;
                o[i] = new object;
            }
            catch (char* e)
            {
                cout <<e;
                object::release();
                i = 0;
            }
        }
        else if (c == '2' && i >= 0)
        {
            delete o[i];
            i--;
        }
    }
    return 0;
}

What am i doing wrong?

EDIT
I have fixed the delete problem. By getting rid of destructor. And explicitly calling release at end of main.

But now my catch block is not catching exception. After allocating 5 objects exception is thrown (as traced through debugger) but not caught. New Changes in the code do not affect the related code.

  • 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-26T07:55:54+00:00Added an answer on May 26, 2026 at 7:55 am

    The infinite loop occurs because the destructor (~object()) is called before object::operator delete(). Your destructor is attempting to delete the most recent object allocated, which calls the destructor on the same object. The operator delete() is never being called.

    I’m not sure what the allocatedObjects achieves here and the code will work without it. Get rid of the release () as well.

    UPDATE

    OK, so there’s a need for the release (exception handling).

    Don’t call release from the destructor. Instead, make the operator delete() function set the allocatedObjects entry to 0, after the calling free (mirroring the operator new()). The release() function is only called during exception handling and makes sure un-freed memory is freed correctly (i.e. loop through allocatedObjects array and free non-zero entries and then setting them to zero.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I previously asked this question here: Stop user from using enter to pass a
I had asked this question Adding more attributes to LINQ to SQL entity Now,
This is a link to a question I had asked two days back, How
I had the same question as was asked in this thread , i.e. I
This question may have been asked before, but I had trouble finding an answer,
I had asked this question once before but has had 0 attention and the
Ok I had asked a question before about how to open fancybox from an
I recently asked a question (and had it answered) here: jQuery Load JSON I
Before I asked this question I had a look through the search results for
I had a problem with the Django tutorial so I asked a question here.

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.