A question about using new instead of malloc

Hello All,
I have a question about the KLU library for LU factorization of sparse matrices.

The KLU library accepts a pointer to a memory allocator function, by default it is malloc().
Then it uses this pointer to allocate the memory required.

I want to extend the library and I now have object of classes. I want to use the operator new instead of malloc to allocate the memory. In the same time I want the new operator to call the constructors of the objects.

Is there a way to do it?

Thank you
Mina
1
2
3
4
5
6
namespace allocator{
  template <class T>
  T* allocate(size_t n){
    return new T[n];
  }
}
new calls the constructors.
Thank you ne555,

I think this is the best solution for my problem... Do you think it would have any side effects?

Thank you
I'm not aware of any. But check out if the library is aware of objects (by instance, that it does not bzero() a chunk)
By the way, T must have a default constructor. If that's an issue, you could wrap std::allocator.

Don't forget to delete []
Topic archived. No new replies allowed.