#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//动态申请二维数组
template <typename T>
T** malloc_Array2D(unsigned short row, unsigned short col)
{
int size = sizeof(T);
int point_size = sizeof(T*);
//先申请内存,其中point_size * row表示存放row个行指针
T **arr = (T **) malloc(point_size * row + size * row * col);
if (arr != NULL)
{
memset(arr, 0, point_size * row + size * row * col);
T *head = (T*)((int)arr + point_size * row);
while (row--)
arr[row] = (T*)((int)head + row * col * size);
}
return (T**)arr;
}
//释放二维数组
void free_Aarray2D(void **arr)
{
if (arr != 0)
free(arr);
arr = NULL;
}//定义滤波器信息结构体
//typedef struct _ArrayFilterMsg
struct _ArrayFilterMsg
{
int m_nFilterNum; //滤波点数
int m_nCounter; //滤波累加计数器
int m_nFilterDataNum; //滤波数组的数据量
};
//定义滤波器信息对象
struct _ArrayFilterMsg m_ArrayData;
double *m_dpFilterArray; //滤波数组
double **m_dpFilterArrayData; //滤波数组中数据存储
double *m_dpFilterArraySum; //滤波数组中数据累加和存储
void InitFilter(/*要初始化的结构体*/_ArrayFilterMsg m_ArrayData,/*滤波点数*/unsigned short nFilterNum, /*滤波计数器*/unsigned short nCounter, /*滤波数组元素数*/unsigned short nFilterDataNum)
{
m_ArrayData.m_nFilterNum = nFilterNum;
m_ArrayData.m_nCounter = 0;
m_ArrayData.m_nFilterDataNum = nFilterDataNum;
//m_ArrayData.m_dpFilterArray = (double*)malloc((m_ArrayData.m_nFilterDataNum)*sizeof(double));
//m_ArrayData.m_dpFilterArrayData = malloc_Array2D<double>(m_ArrayData.m_nFilterNum, m_ArrayData.m_nFilterDataNum);
m_dpFilterArrayData = malloc_Array2D<double>(m_ArrayData.m_nFilterNum, m_ArrayData.m_nFilterDataNum);
m_ArrayData.m_dpFilterArraySum = (double*)malloc((m_ArrayData.m_nFilterDataNum)*sizeof(double));
}
void FreeFilter(/*要初始化的结构体*/_ArrayFilterMsg m_ArrayData)
{
//free_Aarray2D((void **)m_ArrayData.m_dpFilterArrayData);
free_Aarray2D((void **)m_dpFilterArrayData);
m_dpFilterArrayData = NULL;
delete m_ArrayData.m_dpFilterArraySum;
m_ArrayData.m_dpFilterArraySum = NULL;
void FilterArray(double*pData)
{
#if 0
/******************************************滤波开始******************************************/
//对256道数据进行10点滤波
if(m_ArrayData.m_nCounter == m_ArrayData.m_nFilterNum)
{
m_ArrayData.m_nCounter = 0
}
//获取每一包数据的计数率
for(m_Logi = 0; m_Logi<m_ArrayData.m_nFilterDataNum;m_Logi++)
{
m_ArrayData.m_dpFilterArrayData[m_ArrayData.m_nCounter][m_Logi] = pData[m_Logi];
}
//计算指定滤波点数的数据累加和
for(m_Logi = 0; m_Logi<m_ArrayData.m_nFilterNum;m_Logi++)
{
for(int i = 0; i<m_ArrayData.m_nFilterDataNum;i++)
{
m_ArrayData.m_dpFilterArraySum[i] += m_ArrayData.m_dpFilterArrayData[m_Logi][i];
}
}
for(m_Logi = 0; m_Logi<m_ArrayData.m_nFilterDataNum;m_Logi++)
{
pData[m_Logi] = m_ArrayData.m_dpFilterArraySum[m_Logi] / m_ArrayData.m_nFilterNum; //得到滤波后的计数率值
}
m_ArrayData.m_nCounter++;
#endif
/******************************************滤波结束******************************************/
}
int main(int argc, char* argv[])
{
#if 0
printf(" C语言中动态的申请二维数组 malloc free\n");
printf(" -- by MoreWindows( http://blog.csdn.net/MoreWindows ) --\n\n");
printf("请输入行列(页)(以空格分开): ");
int nRow, nCol, nPage;
scanf("%d %d %d", &nRow, &nCol, &nPage);
/***********************************************************演示动态开辟1开始****************************************/
//动态申请连续的二维数组
//int **p = malloc_Array2D<int>(nRow, nCol);
InitFilter(/*要初始化的结构体*/ m_ArrayData,/*滤波点数*/ 10, /*滤波计数器*/0, /*滤波数组元素数*/256);
//为二维数组赋值
int i, j;
for (i = 0; i < nRow; i++)
{
for (j = 0; j < nCol; j++)
{
//p[i][j] = i + j;
//m_ArrayData.m_dpFilterArrayData[i][j] = i +j;
m_dpFilterArrayData[i][j] = i +j;
}
}
//输出二维数组
for (i = 0; i < nRow; i++)
{
for (j = 0; j < nCol; j++)
{
//printf("%4d ", p[i][j]);
//printf("%.1f ",m_ArrayData.m_dpFilterArrayData[i][j]);
printf("%.1f ",m_dpFilterArrayData[i][j]);
}
putchar('\n');
}
// free_Aarray2D((void**)p);
FreeFilter(/*要初始化的结构体*/ m_ArrayData);
/***********************************************************演示动态开辟1结束****************************************/
#endif
return 0;
}
#if 0
//如果要给二维数组(m*n)分配空间,代码可以写成下面:
//char **a;
float **a;
void Init2Array(/*行*/int m, /*列*/int n)
{
// 先分配m个指针单元,注意是指针单元
// 所以每个单元的大小是sizeof(char *)
int i;
a = (float **) malloc(m * sizeof(float * ));
// 再分配n个字符单元,
// 上面的m个指针单元指向这n个字符单元首地址
for(i = 0; i < m; i++)
a[i] = (float * )malloc(n * sizeof(float ));
}
//(注意红色部分)
void Free2Array(/*行*/int m)
{
//释放应该是:
int i;
for(i=0;i<m;i++)
free((void *)a[i]);
free((void *)a);
a = NULL;
}
#endif
int main(int argc, char* argv[])
{
#if 0
printf(" C语言中动态的申请二维数组 malloc free\n");
printf(" -- by MoreWindows( http://blog.csdn.net/MoreWindows ) --\n\n");
printf("请输入行列(页)(以空格分开): ");
int nRow, nCol, nPage;
scanf("%d %d %d", &nRow, &nCol, &nPage);
#if 0
/***********************************************************演示动态开辟2开始****************************************/
printf(" C语言中动态的申请二维数组 malloc free\n");
printf(" -- by MoreWindows( http://blog.csdn.net/MoreWindows ) --\n\n");
printf("请输入行列(以空格分开): ");
int nRow, nCol, nPage;
scanf("%d %d %d", &nRow, &nCol, &nPage);
//char **a;
//创建二维数组
Init2Array(/*行*/nRow, /*列*/nCol);
//为二维数组赋值
int j, i;
for (i = 0; i < nRow; i++)
{
for (j = 0; j < nCol; j++)
{
//p[i][j] = i + j;
//m_ArrayData.m_dpFilterArrayData[i][j] = i +j;
a[i][j] = i +j;
}
}
//输出二维数组
for (i = 0; i < nRow; i++)
{
for (j = 0; j < nCol; j++)
{
//printf("%4d ", p[i][j]);
//printf("%.1f ",m_ArrayData.m_dpFilterArrayData[i][j]);
printf("%.1f ",a[i][j]);
}
putchar('\n');
}
//释放二维数组
Free2Array(nRow);
/***********************************************************演示动态开辟2结束****************************************/
#endif
return 0;
}
#if 1
//如果为三维数组(m*n*p)分配空间呢,应该是:
float ***a;
void Init3Array(/*页*/int nPage,/*行*/int nRow, /*列*/int nCol )
{
int i, j;
a = (float ***) malloc(nPage * sizeof(float ** ));
for(i = 0; i < nPage; ++i)
a[i] = (float **) malloc(nRow * sizeof(float * ));
for(i = 0; i < nPage; ++i)
for(j = 0; j < nRow; ++j)
a[i][j] = (float * )malloc(nCol * sizeof(float ));
}
//释放代码为逆过程,具体代码为:
void Free3Array(/*页*/int nPage,/*行*/int nRow)
{
int i,j;
for(i = 0; i < nPage; ++i)
for(j = 0; j < nRow; ++j)
free((void *)a[i][j]);
for(i = 0; i < nPage; ++i)
free((void *)a[i]);
free((void *)a);
a = NULL;
}int main(int argc, char* argv[])
{
/***********************************************************演示动态开辟3开始****************************************/
#if 1
printf(" C语言中动态的申请二(三)维数组 malloc free\n");
printf(" -- by MoreWindows( http://blog.csdn.net/MoreWindows ) --\n\n");
printf("请输入行列(以空格分开): nPage(页),nRow(行), nCol(列)");
int nPage,nRow, nCol;
scanf("%d %d %d", &nPage,&nRow, &nCol);
//创建三维数组
Init3Array(nPage,nRow,nCol);
//为三维数组赋值
int j, i,k;
for (i = 0; i < nPage; i++)
{
for (j = 0; j < nRow; j++)
{
for(k = 0; k < nCol; k++)
{
//p[i][j] = i + j;
//m_ArrayData.m_dpFilterArrayData[i][j] = i +j;
a[i][j][k] = i +j + k;
}
}
}
//输出三维数组
for (i = 0; i <nPage ; i++)
{
for (j = 0; j <nRow ; j++)
{
for(k = 0; k <nCol ; k++)
{
//printf("%4d ", p[i][j]);
//printf("%.1f ",m_ArrayData.m_dpFilterArrayData[i][j]);
printf("%.1f ",a[i][j][k]);
}
putchar('\n');
}
putchar('\n');
}
//释放三维数组
Free3Array(nPage,nRow);
#endif
/***********************************************************演示动态开辟3结束****************************************/
return 0;
}
#endif
#if TestMallocArray4
//动态申请二维数组
template <typename T>
T** malloc_Array2D(unsigned short row, unsigned short col)
{
int size = sizeof(T);
int point_size = sizeof(T*);
//先申请内存,其中point_size * row表示存放row个行指针
T **arr = (T **) malloc(point_size * row + size * row * col);
if (arr != NULL)
{
memset(arr, 0, point_size * row + size * row * col);
T *head = (T*)((int)arr + point_size * row);
while (row--)
arr[row] = (T*)((int)head + row * col * size);
}
return (T**)arr;
}
//释放二维数组
void free_Aarray2D(void **arr)
{
if (arr != 0)
free(arr);
arr = NULL;
}
//定义滤波器信息结构体
//typedef struct _ArrayFilterMsg
struct _ArrayFilterMsg
{
int m_nFilterNum; //滤波点数
int m_nCounter; //滤波累加计数器
int m_nFilterDataNum; //滤波数组的数据量
double *m_dpFilterArray; //滤波数组
double **m_dpFilterArrayData; //滤波数组中数据存储
double *m_dpFilterArraySum; //滤波数组中数据累加和存储
};
//定义滤波器信息对象
struct _ArrayFilterMsg m_ArrayData;
void InitFilter(/*要初始化的结构体*/_ArrayFilterMsg *m_ArrayData,/*滤波点数*/unsigned short nFilterNum, /*滤波计数器*/unsigned short nCounter, /*滤波数组元素数*/unsigned short nFilterDataNum)
{
m_ArrayData->m_nFilterNum = nFilterNum;
m_ArrayData->m_nCounter = 0;
m_ArrayData->m_nFilterDataNum = nFilterDataNum;
m_ArrayData->m_dpFilterArray = (double*)malloc((m_ArrayData->m_nFilterDataNum)*sizeof(double));
m_ArrayData->m_dpFilterArrayData = malloc_Array2D<double>(m_ArrayData->m_nFilterNum, m_ArrayData->m_nFilterDataNum);
}
void FreeFilter(/*要初始化的结构体*/_ArrayFilterMsg *m_ArrayData)
{
free_Aarray2D((void **)m_ArrayData->m_dpFilterArrayData);
m_ArrayData->m_dpFilterArrayData= NULL;
delete m_ArrayData->m_dpFilterArraySum;
m_ArrayData->m_dpFilterArraySum = NULL;
}
#endif
#if TestMallocArray4
printf(" C语言中动态的申请二维数组 malloc free\n");
printf(" -- by MoreWindows( http://blog.csdn.net/MoreWindows ) --\n\n");
printf("请输入行列(以空格分开): ");
int nRow, nCol;
scanf("%d %d", &nRow, &nCol);
/***********************************************************演示动态开辟4开始****************************************/
//动态申请连续的二维数组
//int **p = malloc_Array2D<int>(nRow, nCol);
InitFilter(/*要初始化的结构体*/ &m_ArrayData,/*滤波点数*/ nRow, /*滤波计数器*/0, /*滤波数组元素数*/nCol);
//为二维数组赋值
int i, j;
for (i = 0; i < nRow; i++)
{
for (j = 0; j < nCol; j++)
{
m_ArrayData.m_dpFilterArrayData[i][j] = i +j;
}
}
//输出二维数组
for (i = 0; i < nRow; i++)
{
for (j = 0; j < nCol; j++)
{
printf("%.1f ",m_ArrayData.m_dpFilterArrayData[i][j]);
}
putchar('\n');
}
FreeFilter(/*要初始化的结构体*/ &m_ArrayData);
/***********************************************************演示动态开辟4结束****************************************/
#endif