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.
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.
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.
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
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.
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 .
Bookmarks