CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Mar 2010
    Location
    Melbourne Australia
    Posts
    454

    looping thrrough 2D array ,

    I have the following code which I got through one of the posts here
    Code:
    #ifndef TDYNAMICARRAY_H
    #define TDYNAMICARRAY_H
    
    namespace Massive 
    {
    	template<class T>
    	T **AllocateDynamic2DArray(int nRows,int nCols)
    	{
    		T **dynamicArray;
    		dynamicArray = new T*[nRows];
    		for(int i=0;i< nRows;i++)
    		{
               dynamicArray[i] = new T[nCols];
    		}
    		return dynamicArray;
    	}
    	template<class T>
        void FreeDynamic2DArray( T ** dArray)
    	{
    		delete [] *dArray;
    		delete [] dArray;
    	}
    
    }
    
    
    #endif
    I wish to know how to traverse or loop through a dynamic 2D array using pointer to pointer as returned by the code above.
    like I would in a static T[20][20] 2D array.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: looping thrrough 2D array ,

    It would just be your usual:
    Code:
    for (int i = 0; i < nRows; ++i)
    {
        for (int j = 0; j < nCols; ++j)
        {
            x[i][j];
        }
    }
    Note that the FreeDynamic2DArray is wrong: AllocateDynamic2DArray called new[] in a loop, hence it should call delete[] in a loop.
    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

  3. #3
    Join Date
    Mar 2010
    Location
    Melbourne Australia
    Posts
    454

    Re: looping thrrough 2D array ,

    Thank you. please post the relevant corrections

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: looping thrrough 2D array ,

    Why don't you make an attempt to correct the code?
    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

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

    Re: looping thrrough 2D array ,

    Quote Originally Posted by aamir121a View Post
    I have the following code which I got through one of the posts here
    Can you give us a link to this post?

    The problem is that you're mixing two different ways to allocate and freeing a 2d array. You're allocating the 2d array the "slow" way, but attempting to delete the array as if it was allocated the "fast" way.

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Mar 2010
    Location
    Melbourne Australia
    Posts
    454

    Re: looping thrrough 2D array ,

    Sorry Paul I can't remember , it was a while a ago , it be great if you can post the fast and corrected version of the above code , when it comes to pointer to pointers I get confused.

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

    Re: looping thrrough 2D array ,

    Quote Originally Posted by aamir121a View Post
    Sorry Paul I can't remember , it was a while a ago , it be great if you can post the fast and corrected version of the above code , when it comes to pointer to pointers I get confused.
    First know what the fast way does.

    There are two calls to new[] done -- one for the row pointers, and another for the entire memory pool. Then you point each row pointer to the correct spot in the pool of memory. That is in essence what is being done -- regardless of the number of rows, two calls to new[] are done.

    Compare that to your loop you wrote -- you're calling new[] for each row of data, so if there are 1,000 rows, you're making 1,000 calls to new[], 10,000 rows, 10,000 calls to new[], etc.
    Code:
    #ifndef TDYNAMICARRAY_H
    #define TDYNAMICARRAY_H
    
    namespace Massive 
    {
    	template<class T>
    	T **AllocateDynamic2DArray(int nRows,int nCols)
    	{
    		T **dynamicArray;
                    // Allocate row pointers
                    dynamicArray = new T*[nRows];
    
                   // Allocate data
                   T* data = new T[nRows * nCols];
    
                  // point each row pointer to the memory pool
    		for(int i=0;i< nRows;i++, data += nCols)
                        dynamicArray[i] = data;
    
    		return dynamicArray;
    	}
    	template<class T>
            void FreeDynamic2DArray( T ** dArray)
    	{
    		delete [] dArray[0];
    		delete [] dArray;
    	}
    }
    #endif
    Regards,

    Paul McKenzie

  8. #8
    Join Date
    Mar 2010
    Location
    Melbourne Australia
    Posts
    454

    Re: looping thrrough 2D array ,

    thank you Paul , ( I have always found this concept to be a bit confusing ) I was doing for a QT project , which involves comparing pixel Data ( QImage ) for two image with the same width and height.
    I was wondering instead of running a nested loop , is there a faster way of analyze pixel data in a more parallel way , for example how do player like vlc do it , they are rendering atleast 25 frames per second .

  9. #9
    Join Date
    Mar 2010
    Location
    Melbourne Australia
    Posts
    454

    Re: looping thrrough 2D array ,

    I was think of spiting a large W by H image into of 4 or 8 smaller images and running them in separate threads , in that case function would return a pointer to pointer array , with each array pointing to a smaller piece . however not too sure of splicing them ( some thing in the line of split frame rendering ) but not the rendering part it self.

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