home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!ngcgate!ngc!alexism
- From: alexism@ngc.com (Alexis Mermet)
- Subject: Re: Operator "new" - how to catch array-allocation?
- Message-ID: <BzDA4t.A6r@ngc.com>
- Organization: Network General Corporation
- References: <1992Dec14.143339.26314@daimi.aau.dk>
- Date: Wed, 16 Dec 1992 19:29:17 GMT
- Lines: 115
-
- Here is a simple C++ program which overides the global new and delete
- operators:
-
- I would have expected that A * a = new A(5) calls the overloaded new
- operator, just as delete [] a should call the overloaded delete operator.
-
- I tried on Borland C++ 3.1 and Microsoft c 7.0 and
- *** THE RESULTS ARE DIFFERENT ***
- This is not argue who has the best C++, I just want to understand what is
- done right, and what is done wrong.
-
-
- Borland:
- ~~~~~~~
- A* a = new A[5]
- The overloaded global new operator is ***not*** called
-
- delete [] a;
- The overloaded global delete operator ***is*** called.
-
- => makes quite a mess since the pointer received by the operator delete
- is not exactly what was given on the delete [] line. Somehow an extra field is
- hidden ( to know the size of the array I suppose ).
-
- Even worse: if you use the new syntax with extra parameters like:
- A *a = new (__FILE__,__LINE__) A[5];
- and you have an operator new ( char* file, int line, size_t size ) then
- this operator new gets called. But the pointer it returns is returned as is to
- the application ( with no offset added to hide the size field ) then when
- the application calls delete [] a, the operator deletes gets called with a
- pointer that has been 'corrected' by the compiler to include this hidden
- field. Therefore, delete screw the whole thing up!
-
- In those conditions it is not possible to implement any reliable memory
- allocation tracking mechanism. There is even no way inside the delete
- operator to know whether this if for an array or a standard delete. Therefore
- there is no workaround.
-
-
- Microsoft:
- ~~~~~~~~~
- The overloaded global new and delete operator are both called.
-
- => new returns an address. The program gets this address plus an offset
- ( for a hidden field possibly like the size of the array ). Then when delete[]
- is called with this same address, delete gets called with the address minus
- the offset, so deletes gets the same address as the one returned by new.
-
- Seems the right way to do it for me.
-
-
- QUESTION:
- --------
- Who or What is right ?
-
-
- ------------------------------>> Test sample <<------------------------
-
- #include <stdio.h>
- #include <malloc.h>
-
- int cstr = 0;
- int dstr = 0;
- int allocated = 0;
- int freed = 0;
-
- void * operator new ( size_t size );
- void operator delete ( void * ptr );
-
- class A {
- public:
- A();
- ~A();
- };
-
- A::A()
- {
- cstr++;
- }
-
- A::~A()
- {
- dstr++;
- }
-
- void * operator new ( size_t size )
- {
- allocated ++;
- void * ptr = malloc( size );
- printf("In new, about to return ptr 0x%lx\n",(unsigned long)ptr);
- return ptr;
- }
-
- void operator delete ( void * ptr )
- {
- freed ++;
- printf("In delete to free 0x%lx\n",(unsigned long)ptr);
- free(ptr);
- }
-
- int main()
- {
- A *pa = new A[5];
- printf("About to free address:0x%lx\n",(unsigned long)pa);
- delete [] pa;
- printf("cstr:%d dstr:%d\n",cstr,dstr);
- printf("alloc:%d free:%d\n",allocated,freed);
- return 0;
- }
-
-
- --
- Alexis MERMET-GRANDFILLES Voice: 415-617-2943
- Software Engineer. R&D Fax: 415-321-0855
- Network General Corp.
-