473,387 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

operator new/delete

I have a question about multiple inheritance, operator new/delete and
ambiguity.
Here is some code that I wrote to analyze this problem.

// test.h
==========================
#include <new>
#include <iostream>
#include "stdlib.h"
class Base1
{
public:
Base1() { cout << "Base1:" << this << endl; }
int hi;
};
class Base2
{
public:
Base2() { cout << "Base2:" << this << endl; }
void* operator new(size_t size) throw (std::bad_alloc) { cout << "operator
new\n"; return malloc(size); }
void operator delete(void* ptr) throw() { cout << "operator delete\n";
free(ptr); }
};
class SubClass : public Base1, public Base2
{
public:
SubClass() { cout << "SubClass:" << this << endl; }
};

// test.cc
====================
#include "test.h"
int main()
{
cout << "\n===================\n";
Base1* b1 = new Base1();
delete b1;
cout << "\n===================\n";
Base2* b2 = new Base2();
delete b2;
cout << "\n===================\n";
SubClass* sc = new SubClass();
delete sc;
cout << "\n===================\n";
}
// result
====================

===================
Base1:0x3cc78

===================
operator new
Base2:0x3cc78
operator delete

===================
operator new
Base1:0x3cc78
Base2:0x3cc7c
SubClass:0x3cc78
operator delete

===================


This was done with gcc version 2.95.3 20010315 (release)
Is that the correct behavior?
Shouldn't it complain and state that "new" or "delete" on SubClass is
ambiguous?
Thanks.
Jul 19 '05 #1
2 5793
skscpp wrote:
I have a question about multiple inheritance, operator new/delete and
ambiguity.
Here is some code that I wrote to analyze this problem.

// test.h
==========================
#include <new>
#include <iostream>
#include "stdlib.h"
class Base1
{
public:
Base1() { cout << "Base1:" << this << endl; }
int hi;
};
class Base2
{
public:
Base2() { cout << "Base2:" << this << endl; }
void* operator new(size_t size) throw (std::bad_alloc) { cout << "operator
new\n"; return malloc(size); }
void operator delete(void* ptr) throw() { cout << "operator delete\n";
free(ptr); }
};
class SubClass : public Base1, public Base2
{
public:
SubClass() { cout << "SubClass:" << this << endl; }
};

// test.cc
====================
#include "test.h"
int main()
{
cout << "\n===================\n";
Base1* b1 = new Base1();
delete b1;
cout << "\n===================\n";
Base2* b2 = new Base2();
delete b2;
cout << "\n===================\n";
SubClass* sc = new SubClass();
delete sc;
cout << "\n===================\n";
}
// result
====================

===================
Base1:0x3cc78

===================
operator new
Base2:0x3cc78
operator delete

===================
operator new
Base1:0x3cc78
Base2:0x3cc7c
SubClass:0x3cc78
operator delete

===================


This was done with gcc version 2.95.3 20010315 (release)
Is that the correct behavior?
Shouldn't it complain and state that "new" or "delete" on SubClass is
ambiguous?
Thanks.


From what I understand, there should not be an ambiguity since Base1 is
using the global new/delete, Base2 is using its classes new/delete, and
Subclass is using the new/delete it inherits from Base2. Since you are
making your own new/delete, you really should be declaring your
destructors to be virtual so that delete works properly.

Hope that helps

Jul 19 '05 #2

"skscpp" <sk*****@hotmail.com> wrote in message news:bo**********@news.lsil.com...

This was done with gcc version 2.95.3 20010315 (release)
Is that the correct behavior?
Shouldn't it complain and state that "new" or "delete" on SubClass is
ambiguous?

What's ambiguous about it? There is no "implicit" operator new defined
for classes (like a copy constructor etc...). It is only when the look up
fails to find the method in the scope of the allocated object that it resorts
to looking at the global one.

In this case operator new is looked up in the scope of SubClass which has
an unambiguous resolution in Base2::operator new.

5.3.4/9 defines this.

Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
by: Marcin Vorbrodt | last post by:
Is there any reason why auto_ptr does not have the cast operator like this: operator void* (); So that one could easily test auto_ptr for NULL ??? Standard does not declare such operator. Is...
1
by: Senthilvel Samatharman | last post by:
I am just curious about the case 3 in the follwouing program. I understand that case 1 is the right way to overload, while Case 2 is erroneous. But i do think that the implementation of operator...
20
by: Ioannis Vranos | last post by:
When we use the standard placement new operator provided in <new>, and not a definition of owr own, isn't a call to placement delete enough? Consider the code: #include <new>
11
by: Jonan | last post by:
Hello, For several reasons I want to replace the built-in memory management with some custom built. The mem management itlsef is not subject to my question - it's ok to the point that I have...
13
by: Amy | last post by:
Hello, We are developing C++ appplications for PDAs where memory is limited, so we want to do memory management by ourselves --- pre-allocated a big chunk and overwrite new and delete to call...
2
by: Shark | last post by:
Hi, if we need to change the behavior of operator new, it is called overriding or overloading? My other question is, if we change the behavior of operator new, do we use malloc to do that or we use...
6
by: Lighter | last post by:
Big Problem! How to overload operator delete? According to C++ standard, "A deallocation function can have more than one parameter."(see 3.7.3.2); however, I don't know how to use an overloaded...
3
by: C++Liliput | last post by:
Hi, I was looking at the implementation of operator new and operator new in gcc source code and found that the implementation is exactly the same. The only difference is that the size_t argument...
8
by: Rahul | last post by:
Please read the following code class Test{ public: void * operator new (size_t t) { return malloc(t); } void operator delete (void *p) { free(p); } };
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.