View more

View more

View more

Image of the Day Submit

IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Array allocation from memory pool

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

7 replies to this topic

#1Misery  Members

Posted 19 January 2012 - 01:19 PM

Hi,

I would like to allocate memory from pool for a matrix. How to do this correctly?
Do I have to allocate the memory for a vector of pointers first? Or just the matrix entries?
I would like to allocate the whole matrix in one memory block.

 float **matrix=NULL;
int Cols=someValue, Rows=someValue;


How to allocate this with memory pool like this:

 void *Allocate(int Size);


regards

#2BeerNutts  Members

Posted 19 January 2012 - 01:59 PM

If you just want a 2d array of floats, then you should just be able to allocate a piece of memory like this:
float **matrix;
int Cols=someValue, Rows=someValue;

matrix = (float **)Allocate(sizeof(float)*Cols*Rows);


Then you have a 2d array you can access via matrix[SomeCol][SomeRow] or even matrix[SomeRow][SomeCol] (assuming SomeRow < Rows and SomeCol < Cols), but just stick to one.
My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

#3SiCrane  Moderators

Posted 19 January 2012 - 02:07 PM

Then you have a 2d array you can access via matrix[SomeCol][SomeRow] or even matrix[SomeRow][SomeCol] (assuming SomeRow < Rows and SomeCol < Cols), but just stick to one.

No, that doesn't perform the array arithmetic necessary to index matrix as a 2D array. You either need to perform the multiplication and addition manually or store an indirection array to the individual elements.

#4Misery  Members

Posted 19 January 2012 - 04:45 PM

Should it be something like this:
int Rows,Cols //have some values
matrix=(float**)Allocate(Rows*sizeof(float*)+Rows*(Cols-1)*sizeof(float));
for (int i=0;i<Rows;i++) matrix[i]=matrix[i*Rows];


?? :]

#5fastcall22  Moderators

Posted 19 January 2012 - 11:52 PM

You're close:
void* mem = malloc( rows*sizeof(float*) + rows*cols*sizeof(float) );
float** mat = (float**)mem;
float* data = (float*)mem + rows;

for ( int row = 0; row < rows; ++row )
mat[row] = data + row*cols;

mat[3][2] = 1.f;


Though you could do it this way:
float* mat = (float*)malloc( sizeof(float)*rows*cols );

mat[ 3*rows + 2 ] = 1.f;


Or this way:
class Matrix {
private:
int rows, cols;
float* data;

public:
Matrix( int rows, int cols ) { resize( rows, cols ); }

void resize( int newRows, int newCols ) { /* ... */ }

float& operator() (int row, int col) { return data[ row*cols + col ]; }
const float& operator() (int row, int col) const { return data[ row*cols + col ]; }
}

void test() {
Matrix mat( 4, 4 );

mat(3, 2) = 1.f;
}


zlib: eJzVVLsSAiEQ6/1qCwoK i7PxA/2S2zMOZljYB1TO ZG7OhUtiduH9egZQCJH9 KcJyo4Wq9t0/RXkKmjx+ cgU4FIMWHhKCU+o/Nx2R LEPgQWLtnfcErbiEl0u4 0UrMghhZewgYcptoEF42 YMj+Z1kg+bVvqxhyo17h nUf+h4b2W4bR4XO01TJ7 qFNzA7jjbxyL71Avh6Tv odnFk4hnxxAf4w6496Kd OgH7/RxC

#6Misery  Members

Posted 20 January 2012 - 12:37 AM

Thanks a lot :]
So allocating a float matrix of size NxN from a pool requires more memory than using new[] operator?
The difference is a vector of pointers.
  int totalsize=sizeof(float*)*Rows+sizeof(float)*Rows*Cols;


#7fastcall22  Moderators

Posted 20 January 2012 - 12:52 AM

So allocating a float matrix of size NxN from a pool requires more memory than using new[] operator?

No. The extra memory comes from setting up the row pointers, which are required if you want to use the matrix[row][col] notation. However, you can avoid the overhead by using a single dimension array and use this notation: matrix[ row*numRows + col ].

EDIT:
I just remembered you can do this:
const int N = 4;
/* 1: */ {
float (*mat)[N] = new float[N][N];
mat[3][2] = 1.f;

delete[] mat;
}

/* 2: */ {
float (*mat)[N] = (float(*)[N]) malloc( sizeof(float)*N*N );
mat[3][2] = 1.f;

free( mat );
}


The best of both worlds: No need for allocating and setting up row pointers, and the mat[row][col] notation.

Edited by fastcall22, 20 January 2012 - 02:08 AM.

zlib: eJzVVLsSAiEQ6/1qCwoK i7PxA/2S2zMOZljYB1TO ZG7OhUtiduH9egZQCJH9 KcJyo4Wq9t0/RXkKmjx+ cgU4FIMWHhKCU+o/Nx2R LEPgQWLtnfcErbiEl0u4 0UrMghhZewgYcptoEF42 YMj+Z1kg+bVvqxhyo17h nUf+h4b2W4bR4XO01TJ7 qFNzA7jjbxyL71Avh6Tv odnFk4hnxxAf4w6496Kd OgH7/RxC

#8Misery  Members

Posted 20 January 2012 - 03:00 AM

Thanks Guys very much
You really helped me a lot!

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.