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 543971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:36:21+00:00 2026-05-13T10:36:21+00:00

I am trying to find dynamically and statically instantiated objects number. I am getting

  • 0

I am trying to find dynamically and statically instantiated objects number. I am getting errors that variable myheap is not declared.

#include<iostream.h>
#include<stdlib.h>

class A {
public:
  static int x;   //To count number of total objects. incremented in constructor
  static int myheap;  //To count number of heap objects. Incremented in overloaded new

  void* operator new(size_t t) {
    A *p;
    p=(A*)malloc(t);
    myheap++;
    return p;
  }

  void operator delete(void *p) {
    free(p);
    myheap--;
  }

  A() {
    x++;
  }

  ~A() {
    x--;
  }
};
int A::x=0;
int A::myheap=0;

int main() {
  A *g,*h,*i;
  A a,c,b,d,e;//Static allocations 5

  g= new A();//Dynamic allocations 3
  h= new A();
  i= new A();

  cout<<"Total"<<A::x<<'\n';

  cout<<"Dynamic";
  cout<<'\n'<<"HEAP"<<A::myheap;

  delete g;
  cout<<'\n'<<"After delete g"<<A::x;
  cout<<'\n'<<"HEAP"<<A::myheap;
  delete h;
  cout<<'\n'<<"After delete h"<<A::x;
  cout<<'\n'<<"HEAP"<<A::myheap;
  delete i;
  cout<<'\n'<<"After delete i"<<A::x;
  cout<<'\n'<<"HEAP"<<A::myheap;
}
  • 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-13T10:36:21+00:00Added an answer on May 13, 2026 at 10:36 am

    Your code is almost correct, but you’re seeing errors about ‘myheap’ because the compiler was confused about earlier errors. Fix the first error first.

    About overloading operator new, there’s more to it than a simple malloc. I have an previous example that may help, but that was global new instead of class-specific.

    Here it is cleaned up: (this compiles and runs)

    #include <iostream>
    #include <memory>
    #include <new>
    #include <stdlib.h>
    
    struct A {
      static int count;
      static int heap_count;
      void* operator new(std::size_t t) {
        void* p = malloc(t);
        if (!p) throw std::bad_alloc();
        heap_count++;
        return p;
      }
      void operator delete(void *p) {
        free(p);
        heap_count--;
      }
      A() {
        count++;
      }
      ~A() {
        count--;
      }
    };
    int A::count = 0;
    int A::heap_count = 0;
    
    int main() {
      using namespace std;
    
      A a, b, c, d, e;
      auto_ptr<A> g (new A), h (new A), i (new A);
    
      cout << "Total: " << A::count << '\n';
      cout << "Dynamic\nHeap: " << A::heap_count << '\n';
      g.release();
      cout << "After delete g: " << A::heap_count << '\n';
      h.release();
      cout << "After delete h: " << A::heap_count << '\n';
      i.release();
      cout << "After delete i: " << A::heap_count << '\n';
      cout << "Heap: " << A::heap_count << '\n';
    
      return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to see how to find a tag that was added dynamically
I'm trying to dynamically create domain objects in Grails and encountered the problem that
Trying to find an example that has css rollover using sprites & sliding door
Trying to find examples of when decorators might be really beneficial, and when not
So im trying to find a script that can start me off with creating
Hello I'm trying to dynamically generate some inputs for my form, but it's not
I'm trying to find a way to replace a node with one that has
Trying to find the best way to dynamically add a direction attribute to each
I am trying to find a way to dynamically add image with some hover
I am trying to find a solution for dynamically loading the updated dll from

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.