Dynamic Two Dimensional Array
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Dynamic Two Dimensional Array

  1. #1
    Join Date
    Mar 2009
    Posts
    2

    Dynamic Two Dimensional Array

    template <typename T>
    T** AllocateDynamicArray(int Rows , int cols)
    {
    T** DynamicArray;
    DynamicArray = new T*[Rows];
    for(int i = 0; i < Rows ; i++)
    {
    DynamicArray[i] = new T [cols];
    return DynamicArray;
    }
    }

    template <typename T>
    void FreeDynamicArray(T** dArray)
    {
    delete []* dArray;

    delete [] dArray;
    }


    i used above code to allocate two dimentional array

    double **a = AllcateDyamicArray<double>(n,m);


    .
    .

    .

    FreeDynamicArray<double>(a);





    this is one method

    and second is


    vector<vector<double>> v(n,vector<double>(m,0));





    which is good method to allocate two dimensional array. b'coz i my project lot of Matrix operation are required.
    so please suggest me any good method.

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Dynamic Two Dimensional Array

    The vector approach has no disadvantage over your home-grown one. However, an option which might be better is the contiguous-memory approach:
    Code:
    vector<double> v_data(n*m);
    vector<double*> v(n);
    for (int i = 0; i < n; i++)
        v[i] = &v_data[i*m];
    You may also wish to investigate the Matrix Template Library. I haven't used it, but it claims high performance and certainly looks decently-designed at first glance.

  3. #3
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Dynamic Two Dimensional Array

    Quote Originally Posted by NandiniC View Post
    vector<vector<double>> v(n,vector<double>(m,0));
    Just FYI, this is dangerous to do with the GNU compiler. I've run into an issue where the grammar will read >> as a bit shift for some reason. Since then I always do 2D vectors like this:

    Code:
    typedef vector<double> DoubleVector;
    vector<DoubleVector> v

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Dynamic Two Dimensional Array

    You can simply include a space between the two >s. I usually do.

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,276

    Re: Dynamic Two Dimensional Array

    Quote Originally Posted by ninja9578
    Just FYI, this is dangerous to do with the GNU compiler. I've run into an issue where the grammar will read >> as a bit shift for some reason.
    Yes, this problem is not just with g++, but with any standard compliant C++ compiler prior to the advent of C++0x where this greedy matching will finally be changed. The typedef can be useful more than as just a way of avoiding this, but otherwise insertion of a space will do: vector<vector<double> >.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,721

    Re: Dynamic Two Dimensional Array

    Visual Studio 2008 is happy with >>
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  7. #7
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Dynamic Two Dimensional Array

    Quote Originally Posted by Lindley View Post
    You can simply include a space between the two >s. I usually do.
    Quote Originally Posted by laserlight View Post
    Yes, this problem is not just with g++, but with any standard compliant C++ compiler prior to the advent of C++0x where this greedy matching will finally be changed. The typedef can be useful more than as just a way of avoiding this, but otherwise insertion of a space will do: vector<vector<double> >.
    Huh, I don't know why I never thought to try that. Oh well, I like the typedef better anyway, looks neater and easier to read.

  8. #8
    Join Date
    Apr 1999
    Posts
    27,427

    Re: Dynamic Two Dimensional Array

    Quote Originally Posted by NandiniC View Post
    template <typename T>
    T** AllocateDynamicArray(int Rows , int cols)
    {
    T** DynamicArray;
    DynamicArray = new T*[Rows];
    for(int i = 0; i < Rows ; i++)
    {
    DynamicArray[i] = new T [cols];
    return DynamicArray;
    }
    }
    What if you had 10,000 rows and 10,000 columns? You are calling "new" 10,001 times. That could lead to memory fragmentation.

    Unless there is a specific reason to dynamically allocate each row, the code you posted only needs two calls to "new". One call allocates the pointers, and the second call allocates the pool of data. Then you loop, pointing the pointers in the memory pool:
    Code:
    template <typename T>
    T** AllocateDynamicArray(int Rows , int cols)
    {
        T** DynamicArray;
        DynamicArray = new T*[Rows];
        T* pool = new T[Rows*cols];
        for(int i = 0; i < Rows ; i++)
        {
            DynamicArray[i] = pool;
            pool += cols;
        }
        return DynamicArray;
    }
    Now it doesn't matter how many rows or columns you have, you still only make two calls to "new". Not only that, I expect this code to be much faster than calling the allocator cols+Rows times.

    On destruction, you have only two calls to delete to make:
    Code:
    template <typename T>
    void FreeDynamicArray(T** dArray)
    {
        delete [] dArray[0];
        delete [] dArray;
    }
    Since dArray[0] pointed to the original start of the pool, you delete that, then you delete the row pointers. Just two calls to delete, regardless of the number of rows and columns.

    Regards,

    Paul McKenzie

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center