• Create Account

### #Actualfastcall22

Posted 20 January 2012 - 02:08 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.

### #8fastcall22

Posted 20 January 2012 - 01:37 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 );
}


No need to store row pointers.

### #7fastcall22

Posted 20 January 2012 - 01:35 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:
/* 1: */ {
float (*mat)[4] = new float[4][4];
mat[3][2] = 1.f;

delete[] mat;
}

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

free( mat );
}


No need to store row pointers.

### #6fastcall22

Posted 20 January 2012 - 01:34 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:
// 1:
float (*mat)[4] = new float[4][4];
mat[3][2] = 1.f;

delete[] mat;

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

free( mat );


No need to store row pointers.

### #5fastcall22

Posted 20 January 2012 - 01:34 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:
// 1:
float (*mat)[4] = new float[4][4];
mat[3][2] = 1.f;

delete[] mat;

// 2:
float (*mat)[4] = (float(*)[4]) malloc( sizeof(float)*4*4 );
free( mat );


No need to store row pointers.

### #4fastcall22

Posted 20 January 2012 - 01:32 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:
float (*mat)[4] = new float[4][4];

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

void* mem = malloc( sizeof(float)*4*4 );
float (*mat)[4] = (float(*)[4]) mem;
free( mat );


PARTNERS