Jump to content
  • Advertisement
Sign in to follow this  
Misery

Array allocation from memory pool

This topic is 2521 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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=matrix[i*Rows];


?? :]

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!