📄 mem.c
字号:
#include "mem.h"
/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
/*
* Mow-Song, Ng 2/9/2002
* msng@mmu.edu.my
* http://www.pesona.mmu.edu.my/~msng
*
* I do not claim copyright to the code, but if you use them or modify them,
* please drop me a mail.
*
*/
/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
/* Allocate 2D byte array */
unsigned char **Byte_Alloc(int rows, int cols)
{
int i;
unsigned char *array;
unsigned char **array2;
// allocate a big chunk
array = (unsigned char *)malloc(cols * rows * sizeof(unsigned char));
if(array==NULL){
return NULL;
}
// now segment it out
array2= (unsigned char **)malloc(rows*sizeof(array));
if (array2==NULL){
return NULL;
}
for (i = 0; i < rows; i++, array+=cols)
array2[i] = array;
// success
return array2;
}
/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
void Byte_Free(unsigned char **array2)
{
free(array2[0]);
}
/* Allocate 2D int array */
int **Int_Alloc(int rows, int cols)
{
int i;
int *array;
int **array2;
// allocate a big chunk
array = (int *)malloc(cols * rows * sizeof(int));
if(array==NULL){
return NULL;
}
// now segment it out
array2= (int **)malloc(rows*sizeof(array));
if (array2==NULL){
return NULL;
}
for (i = 0; i < rows; i++, array+=cols)
array2[i] = array;
// success
return array2;
}
/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
void Int_Free(int **array2)
{
free(array2[0]);
}
/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
/* Allocate 2D double array */
double **Double_Alloc(int rows, int cols)
{
int i;
double *array;
double **array2;
// allocate a big chunk
array = (double *)malloc(cols * rows * sizeof(double));
if(array==NULL){
return NULL;
}
// now segment it out
array2= (double **)malloc(rows*sizeof(array));
if (array2==NULL){
return NULL;
}
for (i = 0; i < rows; i++, array+=cols)
array2[i] = array;
// success
return array2;
}
/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
void Double_Free(double **array2)
{
free(array2[0]);
}
/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
/* Allocate generic 2D array */
void **Mem_Alloc(int rows, int cols, int ElemSize)
{
int i;
BYTE *array;
void **array2;
// allocate a big chunk
array = (BYTE *)malloc(cols*rows*ElemSize);
if(array==NULL){
return NULL;
}
// now segment it out
array2= (void **)malloc(rows*sizeof(array));
if (array2==NULL){
return NULL;
}
for (i = 0; i < rows; i++, array+=cols*ElemSize)
array2[i] = array;
// success
return array2;
}
/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
void Mem_Free(void **array2)
{
free(array2[0]);
}
/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
void *mmalloc(size_t size)
{
void *ptr;
if ((ptr = malloc(size))==NULL){
fprintf(stderr, "Fail to allocate memory.\n");
exit(-1);
}
return ptr;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -