嗨,我有以下代码,我在 100×250 结构数组中分配内存。使用它后,我想释放内存,但我找不到方法。
/*trial programe for storing sample data for each beam of each ping*/
#include <stdio.h>
#include <math.h>
typedef struct{ // in this structure we will define the pointers. so just data type and variable.
int count;
float *samples;
float *deTVG_BS;
} card;
main()
{
//float a,*p;
int i,j,k,l,m,variable;
l=100;
m=250;
variable=10;
card acard[l][m];
//printf("size of acard before memory allocation is %d\n",sizeof(acard));
/* now we will alocote memory for all the pointers we have defined in structure.
* The memory size can be different for each pointer.
* In our case memory size will depends on number of samples for each beam.
* * */
for(i=0;i<l;i++) // i is number of profiles
{
for (j=0;j<m;j++) // j is number of beam
{
acard[i][j].count=variable; //from 1 to 400;
acard[i][j].samples=(float*)calloc(acard[i][j].count,sizeof(float));
acard[i][j].deTVG_BS=(float*)calloc(acard[i][j].count,sizeof(float));
for(k=0;k<acard[i][j].count;k++)
{
acard[i][j].samples[k]=k*1.0;// in reality will be different values
acard[i][j].deTVG_BS[k]=k*100.0; // in reality will be different values
}
}
printf("allocated memeory for profile %d\n",i);
}
for(i=0;i<l;i++) // i is number of profiles
{
for (j=0;j<m;j++)
{
void free(acard[i][j].samples);
void free(acard[i][j].deTVG_BS);
}
}
void free(acard);
}
我不确定我是否应该运行最后一个 for 循环或者只是 free(acard) 就足够了?
非常感谢你。阿南德