473,831 Members | 2,257 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

flexibility of operator new

I'd like to use a few different memory arenas in my current application and
to be able to specify for each allocation where it comes from. Is there any
way I can make operator new take another argument to specify which arena an
object gets allocated from. Or fake it?

e.g. I'd like to be able to write something like this:

A* myA = new_bigheap A;
A* otherA = new_fastheap A;
Jul 22 '05 #1
4 1500
Glen Able wrote:
I'd like to use a few different memory arenas in my current application and
to be able to specify for each allocation where it comes from. Is there any
way I can make operator new take another argument to specify which arena an
object gets allocated from. Or fake it?

e.g. I'd like to be able to write something like this:

A* myA = new_bigheap A;
A* otherA = new_fastheap A;


Yes. If you overload operator new for your class 'A', you may give
it different arguments (in addition to the size it usually takes) and
this will allow you to invoke different operator new functions as you
desire. Something like

A* myA = new (A::bigheap) A;
A* otherA = new (A::fastheap) A;

where 'bigheap' and 'fastheap' are different constants of, say, two
different nested in A types:
class A {
...
class BigHeap {};
static const BigHeap bigheap;
class FastHeap {};
static const FastHeap fastheap;

void* operator new(size_t s, BigHeap const&);
void* operator new(size_t s, FastHeap const&);
};

Disclaimer: this is just to give you an idea; please refer to a good
book for more information.

Victor
Jul 22 '05 #2
> > e.g. I'd like to be able to write something like this:

A* myA = new_bigheap A;
A* otherA = new_fastheap A;
Yes. If you overload operator new for your class 'A', you may give
it different arguments (in addition to the size it usually takes) and
this will allow you to invoke different operator new functions as you
desire. Something like

A* myA = new (A::bigheap) A;
A* otherA = new (A::fastheap) A;

where 'bigheap' and 'fastheap' are different constants of, say, two
different nested in A types:
class A {
...
class BigHeap {};
static const BigHeap bigheap;
class FastHeap {};
static const FastHeap fastheap;

void* operator new(size_t s, BigHeap const&);
void* operator new(size_t s, FastHeap const&);
};

Disclaimer: this is just to give you an idea; please refer to a good
book for more information.


Ah nice, thanks.

Now, I assume I have to either add matching operator deletes and always
remember to call the correct one, or alternatively embed some sort of
id/reference to the correct heap object in the memory returned from new?

Also, I guess I could also do this using a global new instead, so all my
classes can be allocated with this scheme?

Finally, does anyone have a opinion (aesthetic or otherwise) about using a
macro so "new (A::bigheap)" could be the easier-on-the-eye "new_bighea p" ?

thanks,
G.A.
Jul 22 '05 #3
Glen Able wrote:
e.g. I'd like to be able to write something like this:

A* myA = new_bigheap A;
A* otherA = new_fastheap A;

Yes. If you overload operator new for your class 'A', you may give
it different arguments (in addition to the size it usually takes) and
this will allow you to invoke different operator new functions as you
desire. Something like

A* myA = new (A::bigheap) A;
A* otherA = new (A::fastheap) A;

where 'bigheap' and 'fastheap' are different constants of, say, two
different nested in A types:
class A {
...
class BigHeap {};
static const BigHeap bigheap;
class FastHeap {};
static const FastHeap fastheap;

void* operator new(size_t s, BigHeap const&);
void* operator new(size_t s, FastHeap const&);
};

Disclaimer: this is just to give you an idea; please refer to a good
book for more information.

Ah nice, thanks.

Now, I assume I have to either add matching operator deletes and always
remember to call the correct one, or alternatively embed some sort of
id/reference to the correct heap object in the memory returned from new?


I suppose that should do.
Also, I guess I could also do this using a global new instead, so all my
classes can be allocated with this scheme?
I am not sure I understand, but you're probably right.
Finally, does anyone have a opinion (aesthetic or otherwise) about using a
macro so "new (A::bigheap)" could be the easier-on-the-eye "new_bighea p" ?


'new' is a keyword. In most modern editors it would be highlighted. You
would have to edit the set of keywords your editor recognises in order to
get 'new_bigheap' to highlight. If we're talking readability, of course.

Victor
Jul 22 '05 #4
On Tue, 14 Sep 2004 08:17:06 +0100, "Glen Able"
<sm************ *@hotmTHISail.c om> wrote:
Ah nice, thanks.

Now, I assume I have to either add matching operator deletes and always
remember to call the correct one, or alternatively embed some sort of
id/reference to the correct heap object in the memory returned from new?
The latter. If you do:

delete ptr;

then the basic version of operator delete is called. The only time
your overloaded version gets called is in order to release memory
allocated during a new for an object whose constructor threw an
exception.
Also, I guess I could also do this using a global new instead, so all my
classes can be allocated with this scheme?
Yup. You just want to override the basic global new and delete, and an
overload to use your pool system (note global operator new overloads
must be in the global namespace).

You can also put the allocation functions in a base class that you can
derive your objects from if you want to make them allocable using your
special pool system. I agree that the global new overload does seem
like a less invasive method.
Finally, does anyone have a opinion (aesthetic or otherwise) about using a
macro so "new (A::bigheap)" could be the easier-on-the-eye "new_bighea p" ?


The former is namespace aware, etc. Macros are generally considered
evil.

Tom
Jul 22 '05 #5

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

Similar topics

7
8036
by: Paul Davis | last post by:
I'd like to overload 'comma' to define a concatenation operator for integer-like classes. I've got some first ideas, but I'd appreciate a sanity check. The concatenation operator needs to so something like this: 1) e = (a, b, c, d); // concatenate a,b,c,d into e 2) (a, b, c, d) = e; // get the bits of e into a,b,c, and d For example, in the second case, assume that a,b,c,d represent 2-bit integers, and e represents an 8-bit...
1
3883
by: joesoap | last post by:
Hi can anybody please tell me what is wrong with my ostream operator??? this is the output i get using the 3 attached files. this is the output after i run assignment2 -joesoap #include "BitString.h"
1
1261
by: rhettliu | last post by:
Hello ,Every Body! I'm a newbie in software test.Currently, I'm immgrant some code from Oracle to SqlServer,I'm bothered how to test the new code's flexibility to SqlServer. The Situation is: >From Front Control which gather user input to back core which assemble Sql statement.
3
1462
by: Ares Lagae | last post by:
Suppose one is writing a library behavior the classes A1, A2 and A3, which share some common behavior. Some applications will only use one of the classes, while other applications will use all of them. Because the classes share common behavior, the library designed has to choose wether to introduce an abstract base class A, or not. Introducing an ABC has the advantage of run time flexibility, but efficiency suffers (virtual functions &...
6
4432
by: YUY0x7 | last post by:
Hi, I am having a bit of trouble with a specialization of operator<<. Here goes: class MyStream { }; template <typename T> MyStream& operator<<(MyStream& lhs, T const &)
20
3205
by: Joe Fallon | last post by:
My co-worker and I are debating Flexibility vs. Reliability. For example this method is highly flexible: Public Shared Function GetList(ByVal whereClause As String) As String strSQL = "SELECT field1,field2,field3,field4,field5 FROM table1 " If whereClause <> String.Empty Then strSQL &= "WHERE " & whereClause End If Return strSQL End Function
5
2295
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason my C++.NET 2.0 textbook does not have one. I tried to build one using the format as taught in my regular C++ book, but I keep getting compiler errors. Some errors claim (contrary to my book) that you cannot use a static function, that you must...
3
3287
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj, obj2 ;
12
1536
by: loisk | last post by:
Hi, I need to create a filter form that gives a user flexibility in selection any fields in a table and in filtering records based on conditions, such as range. I've read some about a multi-select list box, but it does not give flexibility of choosing any fields. Can anybody help me on this and tell me what subject I should read? Many thanks in advance!
0
9793
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
10777
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
10494
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10534
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10207
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...
1
7748
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
6951
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
5780
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4416
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

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.