🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #3008

Overloading 'operator new': errors in MSVC 2.0 or not?

#3008
From: mike
Date: Fri, 12 Jan 1996 17:20
77 lines
2854 bytes
I think I've discovered some errors in Microsoft Visual C++ 2.0 compiler.

1) Suppose we want to overload global 'operator new':

void* operator new (size_t size, MemoryAllocator* palloc)
{
    // Allocate memory using special allocator
    return palloc->alloc (size);
}

How to delete object allocated by this function?
We can't overload 'operator delete' (according to ARM 5.3.4, 12.5).
We must write something like this:

void Delete_SomeClass (SomeClass* p, MemoryAllocator* palloc)
{
    p->~SomeClass ();    // direct destructor call
    palloc->free (p);    // free memory
}

It works, but it is too tiresome to write such function for every class.
A better way is to use template function:

template <class Type>
void Delete (Type* p, MemoryAllocator* palloc)
{
    p->~Type ();         // direct destructor call - is it allowed here?
    palloc->free (p);    // free memory
}

But if I try to write

    MemoryAllocator alloc;
    SomeClass* p = new (&alloc) SomeClass;
    Delete (p, &alloc);

then compiler writes something like
filename(line#) : error C2300: 'SomeClass' : class does
not have a destructor called '~Type'

Is it an error of compiler? Or direct call of destructor
in template is not allowed? I haven't found something
about it in ARM.


2) Again, let's overload global 'operator new' with default parameter:

void* operator new (size_t size, MemoryAllocator* palloc = NULL)
{
    return palloc ? palloc->alloc (size) : malloc (size);
}

If I write now

    char* p = new char [100];

then compiler (it seems) must invoke operator new (100, NULL). But when I've
inspected assembler listing, I discovered that compiler invokes standard
library version of 'operator new' rather than overloaded one.
To correct it, I have to write

void* operator new (size_t size, MemoryAllocator* palloc)
{
    return palloc ? palloc->alloc (size) : malloc (size);
}
void* operator new (size_t size)
{
    return operator new (size, NULL);
}

Now it works OK. Is it also a compiler error?

Alex Bobkov.
---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID: <m0talmh-000FHlC@redline.ru>
Path: arc1-ca.pugleaf.net!in1.uu.net!bounce-back

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!in1.uu.net!bounce-back
Date: 12 Jan 96 17:20:17 GMT
Approved: fjh@cs.mu.oz.au
Organization: -
Newsgroups: comp.std.c++
Return-Path: <daemon@meeker.UCAR.EDU>
Message-ID: <m0talmh-000FHlC@redline.ru>
From: mike <mike@redline.ru>
X-Original-Date: Fri, 12 Jan 96 19:50:39 -800
X-Mailer: Mozilla/0.94 Beta (Windows)
Subject: Overloading 'operator new': errors in MSVC 2.0 or not?
X-Auth: PGPMoose V1.1 PGP comp.std.c++
 iQBFAgUBMPaYa+EDnX0m9pzZAQF3fwF/fILkqVnfHBrCNkZ95pm5Lphl7ztJUYHk
 1IcFREZ+WYWu1X5fR5i7bC0QgLIRZBRN
 =xb2S