开发者

Static class member declaration error

开发者 https://www.devze.com 2022-12-15 08:49 出处:网络
I am trying to find dynamically and statically instantiated objects number.I am getting errors that variable myheap is not declared.

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;开发者_JAVA技巧
  delete i;
  cout<<'\n'<<"After delete i"<<A::x;
  cout<<'\n'<<"HEAP"<<A::myheap;
}


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


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.


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消