Question about malloc and new
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
Re: Question about malloc and new
I'm not shure that I understand what you are asking
placement new comes to my mind
Code:
#include <cstdlib>
#include <memory>
#include <iostream>
#include <string>
void * my_allocate( size_t sz ) {
// forward to any allocation function
return malloc(sz);
}
class MyClass {
public:
MyClass(): m_str("Instance of MyClass") {}
std::string m_str;
};
int main() {
void * p = my_allocate( sizeof(MyClass) ); // allocate space
MyClass * A = new (p) MyClass; // construct using placement new
std::cout << A->m_str << std::endl;
}
Kurt
Re: Question about malloc and new
Quote:
Originally Posted by
zarzor_2010
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.
malloc does not construct objects, so if the library uses malloc by default, then it probably doesn't create non-POD objects.
It's not clear to me what you are trying to achieve. How do you want to use classes with the library?
Re: Question about malloc and new
Code:
void * p = my_allocate( sizeof(MyClass) ); // allocate space
Please don't do that. sizeof(Class) is horribly wrong.
Re: Question about malloc and new
Quote:
Originally Posted by
Skizmo
Code:
void * p = my_allocate( sizeof(MyClass) ); // allocate space
Please don't do that. sizeof(Class) is horribly wrong.
Apart from potential alignment/design problems, what exactly (out of curiosity) is "horribly wrong" with that?
Re: Question about malloc and new
Quote:
Originally Posted by
zarzor_2010
I want to extend the library
To have any hope of changing the way KLU handles memory internally it must be designed to allow that. And to know you need to get in touch with the KLU programmers.
A much better option is to make a C++ interface based on KLU as it is. You can model it after the already existing interface for Matlab.
Re: Question about malloc and new
Quote:
Originally Posted by
zarzor_2010
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.
I agree with nuzzle. You need to rethink what you're doing and go a step further. Create a C++ class wrapper around the KLU library, and hide all of these ugly details in the wrapper. If you're going to use C++, then take advantage of it.
Trying to get the KLU function to call "new" instead of malloc() is IMO a waste of time, as there is no advantage at all in doing this. Spend your time actually doing something that will be worth it.
I took a look at the KLU library, and it certainly could use a good C++ wrapper. The functions (all starting with klu_, which gives a good hint on how to create such a wrapper) take arguments that could be just members of an object. In addition, the library will suffer a memory leak if you don't clean up what you've allocated -- a good chance to create a wrapper that has a destructor to handle this so that you no longer need to worry about memory leakage.
So on the outside, interfacing to the KLU library looks like you're using C++ (and not making naked 'C' function calls to the library, as is stated in their documentation)
Regards,
Paul McKenzie
Re: Question about malloc and new
Quote:
Originally Posted by
monarch_dodra
Apart from potential alignment/design problems, what exactly (out of curiosity) is "horribly wrong" with that?
I'd say "dangerous" rather than "wrong".
See http://www.parashift.com/c++-faq-lit...html#faq-11.10
Re: Question about malloc and new
Quote:
Originally Posted by
Paul McKenzie
I agree with nuzzle. You need to rethink what you're doing and go a step further. Create a C++ class wrapper around the KLU library, and hide all of these ugly details in the wrapper. If you're going to use C++, then take advantage of it.
Trying to get the KLU function to call "new" instead of malloc() is IMO a waste of time, as there is no advantage at all in doing this. Spend your time actually doing something that will be worth it.
Thank you all for replying.
I think I was not clear of what I need.
The KLU library currently only LU factorize matrices that contain double,integer, complex....etc.
What I want to do is to extend this to matrices that contains sub-matrices. So each entry in the matrix is a smaller matrix by itself. This has a lot of applications.
So briefly, I made a class called submatrix and made all the +-/* operation to it. Then, inside the klu header file I have changed, for instance:
#define Entry double
to
#define Entry submatrix //which is the submatrix class.
This will make the KLU use the submatrix class. However, I am faced with the memory allocation, because KLU uses malloc() by default, which makes sense if we have only double entries.
However, I want to change malloc to new to call the constructor of the "submatrix" class.
In the KLU header file, there is a function called KLU_MALLOC where you can send a pointer to the memory allocation function you want to use instead of malloc.
So, I want to send a pointer to the new operator and then the same time when the KLU_MALLOC uses this function pointer, it would call the constructor.
Thank you
Re: Question about malloc and new
Quote:
Originally Posted by
zarzor_2010
What I want to do is to extend this to matrices that contains sub-matrices. So each entry in the matrix is a smaller matrix by itself. This has a lot of applications.
So briefly, I made a class called submatrix and made all the +-/* operation to it. Then, inside the klu header file I have changed, for instance:
#define Entry double
to
#define Entry submatrix //which is the submatrix class.
I don't know the KLU library, but I think most LU algorithms requires matrix elements to be taken at least from a field. You must use special algorithms with general matrix rings. Hence, even if you managed to make the Entry macro hack to compile and run, the resulting algorithm would not probably work as expected.
Re: Question about malloc and new
Quote:
Originally Posted by
zarzor_2010
So briefly, I made a class called submatrix and made all the +-/* operation to it. Then, inside the klu header file I have changed, for instance:
#define Entry double
to
#define Entry submatrix //which is the submatrix class.
There is no telling if this will work (even just technically, i.e. notwithstanding superbonzo's point) without knowing the KLU library. The issue with malloc is likely just the first issue you've encountered. Next you may run into memcpy's and the like that you'll have to rewrite as well. Before you know it you'll be rewriting the entire library. I'd say your best bet is to check with the authors or active supporters of the library to what extent this is possible.
Re: Question about malloc and new
Quote:
Originally Posted by
zarzor_2010
The KLU library currently only LU factorize matrices that contain double,integer, complex....etc.
I would look at how it handles 'complex' values. This is not a primitive type in C++ so you should be able to handle your class/structure the same way.
Re: Question about malloc and new
Quote:
Originally Posted by
Peter_B
I would look at how it handles 'complex' values. This is not a primitive type in C++ so you should be able to handle your class/structure the same way.
the KLU uses the c99 complex.h header file. So, it declares Entry as double complex.
Quote:
Originally Posted by
D_Drmmr
There is no telling if this will work (even just technically, i.e. notwithstanding superbonzo's point) without knowing the KLU library. The issue with malloc is likely just the first issue you've encountered. Next you may run into memcpy's and the like that you'll have to rewrite as well. Before you know it you'll be rewriting the entire library. I'd say your best bet is to check with the authors or active supporters of the library to what extent this is possible.
the good thing that the KLU does not use memcpy or memset. This is because you can avoid memcpy and memset in the LU factorization algorithm. It is a property of the algorithm. The bad news is that the KLU has no forums or support website. It is a library made by academic people and college students.
One of my friends suggested to use the following function and send a pointer to it to KLU_MALLOC function. But I am not sure if it is a good idea. Can you guys please tell me your opinion.
PHP Code:
namespace allocator{
template <class T>
T* allocate(size_t n){
return new T[n];
}
}
Re: Question about malloc and new
Quote:
Originally Posted by
zarzor_2010
One of my friends suggested to use the following function and send a pointer to it to KLU_MALLOC function. But I am not sure if it is a good idea. Can you guys please tell me your opinion.
The KLU is a C-based library. It knows nothing about objects, constructors, or templates.
What is wrong with providing a simple function that calls operator new, or whatever else you desire? It states in the documentation how to provide your own allocator and deallocator. You don't need to go into esoterics such as templates.
Code:
void *MyAllocator()
{
return new WhateverObject;
}
void MyDealloctor(void *whatever)
{
delete (WnateverObject*)whatever;
}
Regards,
Paul McKenzie
Re: Question about malloc and new
Thank you for all your help.
I think I would go with "Paul McKenzie" way. It seems the best solution for me.
Thank you all