none
declare a matrix (2D array) in manage C++ ? RRS feed

  • Question

  • Hi I have the following unmanage function , I want to convert it to manage. So my quistion is how I declare a matrix in manage code and fill it with zero as default?


    double ** allocateMatrix(const int rows, const int cols)
    {
        double **A = new double*[rows];
        for(int i=0;i<rows;i++)
        {
            A[i] = new double[cols];
            memset(A[i],0,cols*sizeof(double));
        }
        return A;
    }
    Monday, November 9, 2009 4:18 PM

Answers

  • First off, arrays in managed code are always zero initialized - so you don't need the memset.  To allocate it, just do:


    cli::array<double,2>^ allocateMatrix(const int rows, const int cols)
    {
        cli:array<double,2>^ A = gcnew cli::array<double,2>(rows,cols);
        return A;
    }
    Or just create it directly, since it's a one liner (and avoid the method):

    cli::array<double,2>^ myArray = gcnew cli::array<double,2>(rows,cols);
    Note that this is a 2D array.  If you want a jagged array (which is more like your original, where you have an array of arrays), you could do:

    cli::array<cli::array<double>^>^ allocateMatrix(const int rows, const int cols)
    {
        cli:array<cli::array<double>^>^ A = gcnew cli::array<cli::array<double>^>(rows);
        for(int i=0;i<rows;++i)
            A[i] = gcnew cli::array<double>(cols);
        return A;
    }

    Reed Copsey, Jr. - http://reedcopsey.com
    Monday, November 9, 2009 6:26 PM
    Moderator
  • When you're dealing with managed code, the best thing to do is just do:

         A = null;

    Do not call GC::Collect(); 

    There really is no reason to do this - let it do it as needed.  Calling GC.Collect actually usually hurts your performance.  Trust the Garbage Collector - it's an amazing piece of engineering, and does a fantastic job.  If you want to read some more info on this, including the (maybe) one valid reason to call this, I'd recommend reading Rico Mariani's post on GC.Collect


    Reed Copsey, Jr. - http://reedcopsey.com
    Wednesday, November 11, 2009 6:21 PM
    Moderator

All replies

  • First off, arrays in managed code are always zero initialized - so you don't need the memset.  To allocate it, just do:


    cli::array<double,2>^ allocateMatrix(const int rows, const int cols)
    {
        cli:array<double,2>^ A = gcnew cli::array<double,2>(rows,cols);
        return A;
    }
    Or just create it directly, since it's a one liner (and avoid the method):

    cli::array<double,2>^ myArray = gcnew cli::array<double,2>(rows,cols);
    Note that this is a 2D array.  If you want a jagged array (which is more like your original, where you have an array of arrays), you could do:

    cli::array<cli::array<double>^>^ allocateMatrix(const int rows, const int cols)
    {
        cli:array<cli::array<double>^>^ A = gcnew cli::array<cli::array<double>^>(rows);
        for(int i=0;i<rows;++i)
            A[i] = gcnew cli::array<double>(cols);
        return A;
    }

    Reed Copsey, Jr. - http://reedcopsey.com
    Monday, November 9, 2009 6:26 PM
    Moderator
  • Thanks a lot , I used the first way :-)

    Just one more clarification, to delete the matrix (A) , can I use delete , I know that Garbage collector do that, but I want to release memory in C# I used to do the following:

    A= null;
    GC.Collect;

    How should I do this here?
    What from the following is the correct way? :

    1.
        delete A;

    2.

        delete A;
        GC::Collect();

    3.
         A= nullptr;
        GC::Collect();


    Wednesday, November 11, 2009 7:08 AM
  • When you're dealing with managed code, the best thing to do is just do:

         A = null;

    Do not call GC::Collect(); 

    There really is no reason to do this - let it do it as needed.  Calling GC.Collect actually usually hurts your performance.  Trust the Garbage Collector - it's an amazing piece of engineering, and does a fantastic job.  If you want to read some more info on this, including the (maybe) one valid reason to call this, I'd recommend reading Rico Mariani's post on GC.Collect


    Reed Copsey, Jr. - http://reedcopsey.com
    Wednesday, November 11, 2009 6:21 PM
    Moderator