Jump to content

  • Log In with Google      Sign In   
  • Create Account

We need your feedback on a survey! Each completed response supports our community and gives you a chance to win a $25 Amazon gift card!


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.

  • You cannot reply to this topic
7 replies to this topic

#1 Misery   Members   -  Reputation: 317

Like
0Likes
Like

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);

Thanks in advance,
regards

Sponsor:

#2 BeerNutts   Crossbones+   -  Reputation: 3018

Like
1Likes
Like

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)

#3 SiCrane   Moderators   -  Reputation: 9676

Like
1Likes
Like

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.

#4 Misery   Members   -  Reputation: 317

Like
0Likes
Like

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];

?? :]

#5 fastcall22   Crossbones+   -  Reputation: 4478

Like
1Likes
Like

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;
}

c3RhdGljIGNoYXIgeW91cl9tb21bMVVMTCA8PCA2NF07CnNwcmludGYoeW91cl9tb20sICJpcyBmYXQiKTs=

#6 Misery   Members   -  Reputation: 317

Like
0Likes
Like

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;


#7 fastcall22   Crossbones+   -  Reputation: 4478

Like
1Likes
Like

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.

c3RhdGljIGNoYXIgeW91cl9tb21bMVVMTCA8PCA2NF07CnNwcmludGYoeW91cl9tb20sICJpcyBmYXQiKTs=

#8 Misery   Members   -  Reputation: 317

Like
0Likes
Like

Posted 20 January 2012 - 03:00 AM

Thanks Guys very much Posted Image
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.



PARTNERS