📄 alloc.c
字号:
/****************************************************************************
* The following functions dynamically allocate memory, *
* initialize memory and free memory. *
* *
* Authors: Dr. Mohammad W. Ali, Yun Ji, Dr. Todd. H. Hubing *
* Version: 1.1 *
* Last updated: Dec 16, 1998 *
****************************************************************************/
/***************** Function Prototype Definition *****************/
complex **CMPLX_Matrix(int, int);
complex *CMPLX_Vector(int);
double **double_Matrix(int, int);
double ***double_Matrix2(int, int, int);
double *double_Vector( int);
void free_CMPLX_Matrix(complex **,int, int);
void free_CMPLX_Vector(complex *, int);
void free_double_Matrix(double **, int, int);
void free_double_Vector(double *, int);
void free_INT_Matrix(int **, int,int);
void free_INT_Vector(int *, int);
int **INT_Matrix(int, int);
int ***INT_Matrix2( int, int, int);
int *INT_Vector(int);
void Null_double_Matrix(double **,int,int);
void Null_double_Vector(double *, int);
void Null_INTMatrix(int **, int, int);
void Null_INTVector(int *,int);
/*****************************************************************************
Prototype: complex ** CMPLX_Matrix( int row, int column)
Description: To allocate a matrix of complex type
Input value:
int row --- row number of the matrix
int col --- column number of the matrix
Return value: pointer to the matrix
*****************************************************************************/
complex **CMPLX_Matrix(int row, int column)
{
int i;
complex **m;
m=(complex **) malloc((unsigned) row*sizeof(complex*));
for(i=0;i<row;i++)
m[i]=(complex *) malloc((unsigned) column*sizeof(complex));
return m;
}
/*****************************************************************************
Prototype: complex *CMPLX_Vector( int nh )
Description: To allocate a vector of complex type.
Input value:
int nh --- length of the vector
Return value: a pointer to the vector
*****************************************************************************/
complex *CMPLX_Vector(int nh)
{
complex *v;
v=(complex *)malloc((unsigned) nh*sizeof(complex));
return v;
}
/*****************************************************************************
Prototype: double ** double_Matrix(int row, int col)
Description: To allocate a matrix of double type.
Input value:
int row --- the row number of the matrix
int col --- column number of the matrix
Return value: a pointer to the matrix
*****************************************************************************/
double **double_Matrix(int row, int column)
{
int i;
double **m;
m=(double **) malloc((unsigned) row*sizeof(double*));
for(i=0;i<row;i++)
m[i]=(double *) malloc((unsigned) column*sizeof(double));
return m;
}
/*****************************************************************************
Prototype: double *double_Vector(int nh)
Description: To allocate a vector of double type.
Input value:
int nh --- length of the vector
Return value: pointer to the vector
*****************************************************************************/
double *double_Vector( int nh)
{
double *v;
v=(double *)malloc((unsigned) nh*sizeof(double));
return v;
}
/****************************************************************************
Prototype: void free_CMPLX_Vector( complex *v, int nh)
Description: To free a vector of complex type
Input value:
complex * v --- pointer to a vector of complex type
int nh --- length of the vector
Return value: none
****************************************************************************/
void free_CMPLX_Vector(complex *v, int nh)
{
free((char *) v);
}
/*****************************************************************************
Prototype: double *** double_Matrix2( int row, int column, int width) Description: To allocate a three-dimension array of double type.
Input value:
int row --- row number of the matrix
int col --- column number of the matrix
int width --- width of the matrix
Return value: a pointer to the three-dimension array
*****************************************************************************/
double ***double_Matrix2(int row, int column, int width)
{
int i,j;
double ***m;
m=(double ***) malloc((unsigned) row*sizeof(double**));
for(i=0;i<row;i++)
{
m[i]=(double **) malloc((unsigned) column*sizeof(double*));
for(j=0;j<column;j++)
m[i][j]=(double *) malloc((unsigned) width*sizeof(double));
}
return m;
}
/***************************************************************************
Prototype: void free_CMPLX_Matrix( complex **m, int row, int column)
Description: To free a matrix of complex type.
Input value:
complex ** m --- pointer to a matrix of complex type.
int row --- row number of the matrix
int column --- column number of the matrix
Return value: none
***************************************************************************/
void free_CMPLX_Matrix(complex **m, int row, int column)
{
int i;
for(i=0;i<row;i++) free((char*)m[i]);
free((char*) m);
}
/*****************************************************************************
Prototype: void free_double_Matrix( double **m, int row, int column)
Description: To free a matrix of double type.
Input value:
double ** m --- pointer to a matrix of double type.
int row --- row number of the matrix
int column --- column number of the matrix
Return value: none
*****************************************************************************/
void free_double_Matrix(double **m, int row, int column)
{
int i;
for(i=row-1;i>=0;i--) free((char*) m[i]);
free((char*) m);
}
/****************************************************************************
Prototype: void free_double_Vector(double *v, int nh)
Description: To free a vector of double type.
Input value:
double * v --- pointer to a vector of double type
int nh --- length of the vector
Return value: none
****************************************************************************/
void free_double_Vector(double *v, int nh)
{
free((char *) v);
}
/**************************************************************************** Prototype: void free_INT_Matrix( int **m, int row, int column)
Description: To free a matrix of integer type.
Input value:
int ** m --- pointer to a matrix of integer type.
int row --- row number of the matrix
int column --- column number of the matrix
Return value: none
****************************************************************************/
void free_INT_Matrix(int **m, int row, int column)
{
int i;
for(i=row-1;i>=0;i--) free((char*) m[i]);
free((char*) m);
}
/****************************************************************************
Prototype: void free_INT_Matrix( int **m, int row, int column)
Description: To free a matrix of integer type.
Input value:
int ** m --- pointer to a matrix of integer type.
int row --- row number of the matrix
int column --- column number of the matrix
Return value: none
*****************************************************************************/
void free_INT_Vector(int *v, int nh)
{
free((char *) v);
}
/*****************************************************************************
Prototype: int ** INT_Matrix( int row, int col)
Description: To allocates a matrix of integer type
Input value:
int row --- row number of the matrix
int col --- column number of the matrix
Return value: a pointer to the matrix
*****************************************************************************/
int **INT_Matrix(int row, int column)
{
int i;
int **m;
m=(int **) malloc((unsigned) row*sizeof(int));
for(i=0;i<row;i++)
m[i]=(int *) malloc((unsigned) column*sizeof(int));
return m;
}
/*****************************************************************************
Prototype: int *** INT_Matrix2( int row, int column, int width)
Description: To allocate a three dimension array of double type.
Input value:
int row --- row number of the matrix
int col --- column number of the matrix
int width --- width of the matrix
Return value: a pointer to a three-dimension matrix of integer type.
*****************************************************************************/
int ***INT_Matrix2( int row, int column, int width)
{
int i,j;
int ***m;
m=(int ***) malloc((unsigned) row*sizeof(int**));
for(i=0;i<row;i++)
{
m[i]=(int **) malloc((unsigned) column*sizeof(int*));
for(j=0;j<column;j++)
m[i][j]=(int *) malloc((unsigned) width*sizeof(int));
}
return m;
}
/*****************************************************************************
Prototype: void free_CMPLX_Matrix( complex **m, int row, int column)
Description: To free a matrix of complex type.
Input value:
complex ** m --- pointer to a matrix of complex type.
int row --- row number of the matrix
int column --- column number of the matrix
Return value: none
******************************************************************************/
int *INT_Vector(int nh)
{
int *v;
v=(int *)malloc((unsigned) nh*sizeof(int));
return v;
}
/***************************************************************************
Prototype: void Null_double_Matrix( *Vec, int LenX, int LenY)
Description: To initialize a matrix of double type to zero.
Input value:
int * Vec --- pointer to a matrix of double type
int LenX --- row length
int LenY --- column length
Return value: none
***************************************************************************/
void Null_double_Matrix(double **Vec,int LenX, int LenY)
{
int ii,jj;
for(ii=0;ii<LenX;ii++)
for(jj=0;jj<LenY;jj++)
Vec[ii][jj] = 0.0;
}
/***************************************************************************
Prototype: void Null_doubleVector( int *Vec, int LenX)
Description: To initialize a vector of float type to zero.
Input value:
int * Vec --- pointer to a vector of float type
int LenX --- length of the vector
Return value: none
***************************************************************************/
void Null_double_Vector(double *Vec,int LenX)
{
int ii;
for(ii=0;ii<LenX;ii++)
Vec[ii] = 0.0;
}
/****************************************************************************
Prototype: void Null_INTMatrix( int *Vec, int LenX, int LenY)
Description: To initialize a matrix of integer type to zero.
Input value:
int * Vec --- pointer to a matrix of integer type
int LenX --- row length
int LenY --- column length
Return value: none
****************************************************************************/
void Null_INTMatrix(int **Vec,int LenX,int LenY)
{
int ii,jj;
for(ii=0;ii<LenX;ii++)
for(jj=0;jj<LenY;jj++)
Vec[ii][jj] = 0;
}
/****************************************************************************
Prototype: void Null_INTVector( int *Vec, int LenX)
Description: To initialize a vector of integer type to zero.
Input value:
int * Vec --- pointer to a vector of integer type
int LenX --- length of the vector
Return value: none
*****************************************************************************/
void Null_INTVector(int *Vec,int LenX)
{
int ii;
for(ii=0;ii<LenX;ii++)
Vec[ii] = 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -