By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,925 Members | 1,164 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,925 IT Pros & Developers. It's quick & easy.

allocation and initialization

P: n/a
In one section of my code, the allocating of memory is chewing up a
significant amount of time. I have solved most of the problem by
creating a custom memory allocator which uses memory pools.

Anyway, my problem is I don't now how to use this custom allocator for
non-builtin types. So for things like ints, doubles, etc this works
great but if I have a class called ClassA can I still do this?

Example:

// For basic types

varx = (int*)myallocator(10*sizeof(int));
// For ClassA

varx = (ClassA*)myallocator(10*sizeof(ClassA));
// but varx's constructor never gets called????

How do I use my custom allocator and initialize the object?

I can't globally overload new because I only want the special allocator
used in the one small area of my code. I can't overload new for
'ClassA' because of the same reason.

Nathan Bullock

Jul 28 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
na*****************@yahoo.ca wrote:
In one section of my code, the allocating of memory is chewing up a
significant amount of time. I have solved most of the problem by
creating a custom memory allocator which uses memory pools.

Anyway, my problem is I don't now how to use this custom allocator for
non-builtin types. So for things like ints, doubles, etc this works
great but if I have a class called ClassA can I still do this?

Example:

// For basic types

varx = (int*)myallocator(10*sizeof(int));
// For ClassA

varx = (ClassA*)myallocator(10*sizeof(ClassA));
// but varx's constructor never gets called????

How do I use my custom allocator and initialize the object?

I can't globally overload new because I only want the special
allocator used in the one small area of my code. I can't overload new
for 'ClassA' because of the same reason.


Unless you use 'new' operator, the constructor is not going to be called.
If you want to allocate some memory yourself, then you are obligated to
use "placement new" to initialise the objects.

varx = new (myallocator(10*sizeof(ClassA))) ClassA[10];

V
Jul 28 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.