I am reading the source code of the MESCHACH library for matrix and vector computations. Here is the data structure used in the library:
/* matrix definition */
typedef struct {
unsigned int m, n;
unsigned int max_m, max_n, max_size;
Real **me,*base; /* base is base of alloc'd mem */
} MAT;
In here, what is the use of *base
in the structure ? I mean **me
is the one containing the matrix and the upper values contain its dimensions, what does the base
hold.
Code for allocating memory for the matrix:
MAT *m_get(int m, int n)
{
MAT *matrix;
int i;
if (m < 0 || n < 0)
error(E_NEG,"m_get");
if ((matrix=NEW(MAT)) == (MAT *)NULL )
error(E_MEM,"m_get");
else if (mem_info_is_on()) {
mem_bytes(TYPE_MAT,0,sizeof(MAT));
mem_numvar(TYPE_MAT,1);
}
matrix->m = m; matrix->n = matrix->max_n = n;
matrix->max_m = m; matrix->max_size = m*n;
#ifndef SEGMENTED
if ((matrix->base = NEW_A(m*n,Real)) == (Real *)NULL )
{
free(matrix);
error(E_MEM,"m_get");
}
else if (mem_info_is_on()) {
mem_bytes(TYPE_MAT,0,m*n*sizeof(Real));
}
#else
matrix->base = (Real *)NULL;
#endif
if ((matrix->me = (Real **)calloc(m,sizeof(Real *))) ==
(Real **)NULL )
{ free(matrix->base); free(matrix);
error(E_MEM,"m_get");
}
else if (mem_info_is_on()) {
mem_bytes(TYPE_MAT,0,m*sizeof(Real *));
}
#ifndef SEGMENTED
/* set up pointers */
for ( i=0; i<m; i++ )
matrix->me[i] = &(matrix->base[i*n]);
#else
for ( i = 0; i < m; i++ )
if ( (matrix->me[i]=NEW_A(n,Real)) == (Real *)NULL )
error(E_MEM,"m_get");
else if (mem_info_is_on()) {
mem_bytes(TYPE_MAT,0,n*sizeof(Real));
}
#endif
return (matrix);
}
Why do they first allocate base and use it to allocate me
? Also, if you've read the source code, please tell me the use of SEGMENTED in this library. The declaration is in configure
file.
The matrix structure is defined in matrix.h
and m_get() is in memory.c
.