-
January 3rd, 2013, 01:41 AM
#1
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.
-
January 3rd, 2013, 02:48 AM
#2
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.
-
January 3rd, 2013, 03:15 AM
#3
Re: looping thrrough 2D array ,
Thank you. please post the relevant corrections
-
January 3rd, 2013, 04:41 AM
#4
Re: looping thrrough 2D array ,
Why don't you make an attempt to correct the code?
-
January 3rd, 2013, 05:44 AM
#5
Re: looping thrrough 2D array ,
Originally Posted by aamir121a
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
-
January 3rd, 2013, 07:37 PM
#6
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.
-
January 3rd, 2013, 08:50 PM
#7
Re: looping thrrough 2D array ,
Originally Posted by aamir121a
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
-
January 4th, 2013, 01:18 AM
#8
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 .
-
January 4th, 2013, 01:28 AM
#9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|