CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    May 2012
    Posts
    11

    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

  2. #2
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284

    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

  3. #3
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Question about malloc and new

    Quote Originally Posted by zarzor_2010 View Post
    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?
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  4. #4
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    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.

  5. #5
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Question about malloc and new

    Quote Originally Posted by Skizmo View Post
    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?
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  6. #6
    Join Date
    May 2009
    Posts
    2,413

    Re: Question about malloc and new

    Quote Originally Posted by zarzor_2010 View Post
    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.
    Last edited by nuzzle; May 12th, 2012 at 11:51 PM.

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Question about malloc and new

    Quote Originally Posted by zarzor_2010 View Post
    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

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Question about malloc and new

    Quote Originally Posted by monarch_dodra View Post
    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
    Victor Nijegorodov

  9. #9
    Join Date
    May 2012
    Posts
    11

    Re: Question about malloc and new

    Quote Originally Posted by Paul McKenzie View Post
    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

  10. #10
    Join Date
    Oct 2008
    Posts
    1,456

    Re: Question about malloc and new

    Quote Originally Posted by zarzor_2010 View Post
    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.

  11. #11
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Question about malloc and new

    Quote Originally Posted by zarzor_2010 View Post
    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.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  12. #12
    Join Date
    Jan 2009
    Posts
    596

    Re: Question about malloc and new

    Quote Originally Posted by zarzor_2010 View Post
    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.

  13. #13
    Join Date
    May 2012
    Posts
    11

    Re: Question about malloc and new

    Quote Originally Posted by Peter_B View Post
    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 View Post
    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>
      
    Tallocate(size_t n){
        return new 
    T[n];
      }


  14. #14
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Question about malloc and new

    Quote Originally Posted by zarzor_2010 View Post
    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
    Last edited by Paul McKenzie; May 14th, 2012 at 10:15 AM.

  15. #15
    Join Date
    May 2012
    Posts
    11

    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

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured