473,405 Members | 2,349 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,405 software developers and data experts.

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 1480
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_bigheap" ?

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_bigheap" ?


'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.com> 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_bigheap" ?


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
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...
1
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...
1
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:...
3
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...
6
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
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...
5
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...
3
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,...
12
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
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...
0
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,...

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.