473,804 Members | 3,308 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<iostre am>

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 2040
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.dynam ic.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
3054
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 a single collections type, I should be proposing a new "namespaces" module instead. Some of my reasons: (1) Namespace is feeling less and less like a collection to me. Even though it's still intended as a data-only structure, the use cases...
2
5409
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 virtual destructor. The class with the virtual destructor has AddRef() and Release() methods, and Release() ultimately does a "delete this". Can that "delete this" statement somehow find the right delete method? If it does, it involves a downcast...
1
3851
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 are quite a few things that I learned that I did not know before. For example, while I knew that the new and delete operators can be overloaded for classes, I did not know that that the global new and delete operators can also be overloaded. ...
20
4158
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
5432
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. Given a choice between naming functions at the global scope with subsystem prefixes (e.g. CTNode, CPNode) vs. namespaces (Time::Node, Place::Node) we'll use the global namespace. Prefixes they understand, and prefixes are short and do the job. ...
7
7269
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), sales_amount NUMBER(10), sales_date DATE) PARTITION BY LIST(sales_state) (
12
1620
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 delete. how does the delete operator knows the number of object for which destructor has
10
3564
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 should have all primary keys included. So, I created table T1 again with col2 as the partitioning key. Now, I do not have col1 as the primary key. When I try to create col1 as the primary key, I get the following error: 1 The primary key, each...
10
2101
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 demands its template parameter(s) to meet certain requirements. For example simple sorting function template <typename Tvoid sort(T* t, int N) { for (int i=0;i<N-1; i++)
0
9706
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10571
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10075
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9143
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7615
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6851
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5520
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4295
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.