Topic: Overloading new to count objects


Author: ledrecrg@aston.ac.uk (CRG LE-DRESSAY)
Date: Sun, 26 Jun 1994 14:32:19 GMT
Raw View
hello,

I am trying to overload new and delete to check if all objects created with new are deallocated at the end of the program.

a call to new increases the counter new_counter by 1.
a call to delete increases the counter delete_counter by 1.

here is the program:

------- START OF PROGRAM main.cc --------------
#include <iostream.h>
#include <new.h>
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>

int new_count=0;
int delete_count=0;

class foo
{
public:
  int a;
  foo()  { printf("foo created\n"); }
  ~foo() { printf("foo destroyed\n"); }
};

void *
operator new(size_t sz)
{
  ++new_count;
  printf("new called (%d),size %d\n",new_count,sz);
  return malloc(sz);
}

void
operator delete(void* p)
{
  ++delete_count;
  printf("delete called (%d)\n",delete_count);
  free((char*)p);
}

void
report()
{
  printf("new : %d\n",new_count);
  printf("delete : %d\n",delete_count);
}



main()
{
  printf("Start of main\n");
  atexit(report);

  foo f1;
  int a=5;

  cout << a << '\n' << flush;

  foo* pf1=new foo;
  delete pf1;
  foo* pf2=new foo[5];
  delete[] pf2;
  printf("End of main\n");
}
----------- END OF PROGRAM main.cc ------------------

when I run it, I have the following output.


new called (1),size 64
new called (2),size 64
new called (3),size 64
new called (4),size 64
Start of main
foo created
new called (5),size 1024
5
new called (6),size 4
foo created
foo destroyed
delete called (1)
new called (7),size 20
foo created
foo created
foo created
foo created
foo created
foo destroyed
foo destroyed
foo destroyed
foo destroyed
foo destroyed
delete called (2)
End of main
foo destroyed
new : 7
delete : 2


As you can see 4 objects of size 64 are created before main (I have no idea what they are) and are not destroyed before the call to report. cout is created (statement cout << a << '\n' flush;) but is not destroyed before the call to report.

As you see, I have problems only with static objects.
What I would like to do is

--> typical execution of a program

creation of static objects
call to main
end of main
destruction of static objects
--> I want function report to be called here

Do you have any ideas to make it work?

Regards,

Cedric

---
Cedric Le Dressay, MSc SEA student, Aston University, Birmingham, UK.
            Institut d'Informatique d'Entreprise (IIE), Evry, France.






Author: hare@opal.xilinx.com (H.K. Verma)
Date: Mon, 27 Jun 1994 20:52:33 GMT
Raw View
In article <Cs0Dpv.4E5@aston.ac.uk>, ledrecrg@aston.ac.uk (CRG
LE-DRESSAY) writes:

|>
|> void *
|> operator new(size_t sz)
|> {
|>   ++new_count;
|>   printf("new called (%d),size %d\n",new_count,sz);
|>   return malloc(sz);
|> }
|>
|> void
|> operator delete(void* p)
|> {
|>   ++delete_count;
|>   printf("delete called (%d)\n",delete_count);
|>   free((char*)p);
|> }
|>
|> void
|> report()
|> {
|>   printf("new : %d\n",new_count);
|>   printf("delete : %d\n",delete_count);


|> foo destroyed
|> foo destroyed
|> foo destroyed
|> foo destroyed
|> foo destroyed
|> delete called (2)
|> End of main
|> foo destroyed
|> new : 7
|> delete : 2
|>
|>
|> As you can see 4 objects of size 64 are created before main (I have
|> no idea what they are) and are not destroyed before the call to
|> report. cout is created (statement cout << a << '\n' flush;) but is
|> not destroyed before the call to report.
|>
|> As you see, I have problems only with static objects.
|> What I would like to do is
|>
|> --> typical execution of a program
|>
|> creation of static objects
|> call to main
|> end of main
|> destruction of static objects
|> --> I want function report to be called here
|>
|> Do you have any ideas to make it work?

What probably is happening is like ,
 You are overloading global  new and delete. This means that the
any allocation is always performed using this new. This also means that
any allocation of system before the real program starts is by using your
new.
 Instead if you make new and delete static then probably the problem
can be overcome,
 hare..