allocation and initialization

  • Thread starter nathan_kent_bullock
  • Start date
N

nathan_kent_bullock

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
 
V

Victor Bazarov

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top