473,395 Members | 1,468 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,395 software developers and data experts.

Program namespaces partitioning and new/delete operators

Global new and delete operators can be overloaded to suite particulars
needs. Typically they are overloaded to insert useful debugging/trace
informations. What I would to discuss here concerns the possibility of
overload these operators not at the global level namespace but
into different program namespaces.

For instances:
#include<iostream>

using namespace std;

namespace A {

void* operator new(size_t size) {

cout << "New A " << endl;

return ((void*) malloc(size * sizeof(char)));
}

int f() {

int *x = new int(0);
int y = *x;

delete x;

return y;
}

}

namespace B {

void* operator new(size_t size) {

cout << "New B " << endl;

return ((void*) malloc(size * sizeof(char)));
}

int f() {

int *x = new int(1);
int y = *x;

delete x;

return y;
}

}

int main() {

cout << "A::f() " << A::f() << endl;

cout << "B::f() " << B::f() << endl;
}

The idea is that we can partition the program into different
logical namespaces that share a common memory allocation policy,
instead of redefining new/delete operators for any object
we think should have a different one.
Specialized and Local allocation policies have been proved to be
very useful due to the lack of specific optimized capabilites of the
std new operator in many context (see: Alexandrescu MCPPD 4.1)
This can be useful also when we have to plugin with third
party (open source - we need access to complete srcs) softwares.
We can add our allocation support simply embedding them
into the logical namespace that suites better that library.
Namespaces can so be imagined in a class like hierarchy
in which we have:

(here new/delete operators were redifined to give a
global general behaviour)
Global Namespace

(here new/delete operators were redifined to give global
general optimizations)
--> Memory Mamagement Allocation Namespace

(here new/delete operators were redifined to give specific
component optimizations)
--> Program Component 1
..
..
..

(here new/delete operators were redifined to give specific
component optimizations)
--> Program Component N

(here new/delete operators were redifined to give debug information)
--> Debugging Information Namespace

(here new/delete operators were redifined to give specific
component debugging information)
--> Program Component 1
..
..
..

(here new/delete operators were redifined to give specific
component debugging information)
--> Program Component N

}

and so on...

Have you already experienced something like that?
And in which context?

Thanks,

Gianguglielmo

Jul 22 '05 #1
4 2016
GianGuz wrote:
Global new and delete operators can be overloaded to suite particulars
needs. Typically they are overloaded to insert useful debugging/trace
informations. What I would to discuss here concerns the possibility of
overload these operators not at the global level namespace but
into different program namespaces.
The first thing to mention, in case you don't realise this, is that you
cannot overload operators new and delete at any namespace scope except
the global one. If your compiler lets you, it is a non-standard
extension. I tried it for fun, and got the following:
GCC: compiles and works as you expect (e.g. calls the namespace versions)
VC7.1: compiles but calls ::operator new in each case
Como: diagnoses the error correctly
Have you already experienced something like that?
And in which context?


Well, it's all a bit academic since you can't do it, but in any case I
don't like the idea of tying namespace to allocation strategy - the two
are pretty orthogonal.

Tom
Jul 22 '05 #2
My compiler (gcc 3.3.4) allowed it. It is strange that this feature is
considered non-standard.
Why namespace overload of that operators should be forbidden?!
Gianguglielmo

Jul 22 '05 #3
GianGuz wrote:
My compiler (gcc 3.3.4) allowed it. It is strange that this feature is
considered non-standard.
Why namespace overload of that operators should be forbidden?!


How should operator new be looked up? Which namespace should be selected
for the new call? The one in which the call is made or the one of which
the type being created is a member? What about built in types?

I think it would be far too error prone, bearing in mind that new and
delete calls must be perfectly matched.

Tom
Jul 22 '05 #4
GianGuz wrote:
My compiler (gcc 3.3.4) allowed it. It is strange that this feature is
considered non-standard.
Why namespace overload of that operators should be forbidden?!


See 3.7.3.1[1] Allocation functions [basic.stc.dynamic.allocation]

"An allocation function shall be a class member function or a global
function; a program is ill-formed if an allocation function is
declared in a namespace scope other than global scope or declared
static in global scope."

Because they are a basic feature, being able to overload them at
namespace level would greately complicate things both for the compiler
and the user. For example,

int *ptr = 0;

namespace N
{
void *operator new(std::size_t s)
{
// return whatever
}

void operator delete(void *p)
{
// whatever
}

void f()
{
ptr = new int; // N::new or ::new ?
}
}
int main()
{
N::f();

delete ptr; // N::delete or ::delete
}

What about the fact that namespaces are open for modifications in other
translation units? Or ADL ?

As you see, this brings many problems for relatively small benefits.
Jonathan
Jul 22 '05 #5

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

Similar topics

18
by: Steven Bethard | last post by:
In the "empty classes as c structs?" thread, we've been talking in some detail about my proposed "generic objects" PEP. Based on a number of suggestions, I'm thinking more and more that instead of...
2
by: Ian McBride | last post by:
(was: delete() confusion) I have a class with multiple base classes. One of these base classes (base1) has its own new/delete operators and nothing else. Another base class (base 2) has a...
1
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents an excellent discussion of overloaded new and delete operators. In fact there...
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>
15
by: Stuart | last post by:
I work in a small company with developers who do not like to use "new" features when they find the old ones sufficient. e.g. given a choice between a class and a namespace, they'll pick class. ...
7
by: Jane | last post by:
In Oracle we can partition a table as follows. What is the equivalent in DB2? CREATE TABLE sales_list (salesman_id NUMBER(5), salesman_name VARCHAR2(30), sales_state VARCHAR2(20),...
12
by: ravinderthakur | last post by:
hi experts, i have few questions regarding the delete operator in c++. why does c++ have to operators for deleting memeory viz delete and delete. why cannnot delete be used insted of...
10
by: shsandeep | last post by:
DB2 V8.2 (not Viper yet and no range partitioning!!) I have created a table T1 (col1, col2) with col1 as the primary key. When I try to create a partitioning key on col2, it gives me error that it...
10
by: Pavel Shved | last post by:
Is there a program of wide use that automatically extracts template parameters requirements from source code of template functions? I mean the following: assume we have a template function that...
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.