Array allocation from memory pool

Started by
6 comments, last by Misery 12 years, 3 months ago
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
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.

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)


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


?? :]
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;
}
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;
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.
Thanks Guys very much biggrin.png
You really helped me a lot!

This topic is closed to new replies.

Advertisement