tags:

views:

114

answers:

4

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;
}
A: 

It should be A::myheap.

Also, your operator new should invoke the constructor: You're right, you just need to return the pointer to the newly allocated object.

void * operator new(size_t t)
{
 A *p = (A*)malloc(t);
 myheap++;
 return p;
}
Igor Zevaka
Ohhhh....Thanks....missed....
Sandeep
new() also needs to return the object that was new'd (p).
Dan Breslau
Igor...I think Constructor is not called explicitely as it is called implicitely.
Sandeep
No! The new operator calls the ctor, your operator new overload doesn't even know the right ctor to use!
Roger Pate
Thanks Roger for clarification
Sandeep
A: 

You don't have a local named myheap but you do have a class-scoped static variable named myheap. Thus you need A::myheap. But really, myheap and x should be private and you should have a static getx and static getmyheap public methods defined. And, of course, x should have a better name.

Jason
@Roger Pate: He _was_ talking about in `main` where he simply had `myheap` (instead of `A::myheap`) and that is what I was referring to. His code has since been edited which is grossly out of line as it renders answers irrelevant and leads to confusions such as this one. Note that he has left a comment to his question "After fixing `myheap` to `A::myheap` it worked" supporting my statements here.
Jason
Ah, I missed that it was edited. Apologies.
Roger Pate
A: 

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;
}
Roger Pate
You know, I *just* realized you were the OP on that linked question. ><
Roger Pate
A: 

Sandeep,

The reason you were getting a core dump when new was not returning p is because your delete function tries to free the pointer you pass in.

Since new wasn't returning p, the value being sent to delete() is either NULL or un-initialized. Calling free with a pointer that is NULL or a random value from the stack will cause your program to crash.

Best,

Sam

Sam Post