无法找出C程序崩溃的原因
在编写FIR滤波器的C程序时,遇到了程序崩溃的问题。 奇怪的是在调试模式下,没有发现任何奇怪的东西。代码运行正常。 但是在运行时崩溃。使用的IDE是代码块 请查找下面的代码片段 main.c无法找出C程序崩溃的原因,c,C,在编写FIR滤波器的C程序时,遇到了程序崩溃的问题。 奇怪的是在调试模式下,没有发现任何奇怪的东西。代码运行正常。 但是在运行时崩溃。使用的IDE是代码块 请查找下面的代码片段 main.c #include "common.h" #include "carrier_NCO_generation.h" #include "lfsr.h" int main() { int *nco_out = NULL,*noise_out = NULL,*carrier_gen_I = NULL,*c
#include "common.h"
#include "carrier_NCO_generation.h"
#include "lfsr.h"
int main()
{
int *nco_out = NULL,*noise_out = NULL,*carrier_gen_I = NULL,*carrier_gen_Q = NULL,*inp_data_I = NULL,*inp_data_Q = NULL;
int *complex_product_I = NULL,*complex_product_Q = NULL,*FIR_I_Data = NULL,*FIR_Q_Data = NULL;
int i;
nco_out = (int *)malloc(SAMPLES*sizeof(int));
noise_out = (int *)malloc(SAMPLES*sizeof(int));
carrier_gen_I = (int *)malloc(SAMPLES*sizeof(int));
carrier_gen_Q = (int *)malloc(SAMPLES*sizeof(int));
inp_data_I = (int *)malloc(SAMPLES*sizeof(int));
inp_data_Q = (int *)malloc(SAMPLES*sizeof(int));
complex_product_I = (int *)malloc(SAMPLES*sizeof(int));
complex_product_Q = (int *)malloc(SAMPLES*sizeof(int));
FIR_I_Data = (int *)malloc(SAMPLES*sizeof(int));
FIR_Q_Data = (int *)malloc(SAMPLES*sizeof(int));
fileRead(1,SAMPLES,inp_data_I,"rf_sam_i.txt",1);
fileRead(1,SAMPLES,inp_data_Q,"rf_sam_q.txt",1);
carrier_NCO(nco_out);
lfsr(noise_out);
carrier_generation(nco_out,noise_out,carrier_gen_I,carrier_gen_Q);
frequency_transpose(carrier_gen_I,carrier_gen_Q,inp_data_I,inp_data_Q,complex_product_I,complex_product_Q);
fir(complex_product_I,complex_product_Q,FIR_I_Data,FIR_Q_Data);
/*
File Record
*/
fileRecord(1,SAMPLES,nco_out,"Carrier_NCO_out.txt",0);
free(nco_out);
fileRecord(1,SAMPLES,noise_out,"Carrier_NOISE_out.txt",0);
free(noise_out);
fileRecord(1,SAMPLES,carrier_gen_I,"Carrier_gen_I_out.txt",0);
free(carrier_gen_I);
fileRecord(1,SAMPLES,carrier_gen_Q,"Carrier_gen_Q_out.txt",0);
free(carrier_gen_Q);
fileRecord(1,SAMPLES,complex_product_I,"Freq_Trans_I_out.txt",0);
free(complex_product_I);
fileRecord(1,SAMPLES,complex_product_Q,"Freq_Trans_Q_out.txt",0);
free(complex_product_Q);
fileRecord(1,SAMPLES,FIR_I_Data,"FIR_I_Data_out.txt",0);
free(FIR_I_Data);
fileRecord(1,SAMPLES,FIR_Q_Data,"FIR_Q_Data_out.txt",0);
free(FIR_Q_Data);
return 0;
}
问题在于FIR滤波器,代码如下所示
杉木C
#包括“fir.h”
无效数组_乘法(int*数组_数据,int系数[],int大小)
{
int i=0;
对于(i=0;i=1048448))
{
返回max_postve;
}
否则如果(数字<0)
{
数字=((数字)*(-1));
neg_标志=1;
}
位_7=位提取(数字,1,7);
编号=编号和掩码编号;
如果(第7位)
{
数字=数字+128;
}
如果(负标志==1)
{
数字=(数字*(-1));
}
返回号码;
}
第8位的整数对称饱和(整数)
{
int mask_=0x7F,neg_标志=0,max_negtve=-127,max_postve=128;
如果((数字<0)和((数字0)和((数字>=最大值))
{
返回max_postve;
}
else if(数字==0)
{
返回0;
}
否则如果(数字<0)
{
数字=((数字)*(-1));
neg_标志=1;
}
编号=编号,并用;
如果(数字==0)
{
数字=最大值;
}
如果(负标志==1)
{
数字=(数字*(-1));
}
返回(int)编号;
}
无效fir(int*复数I_数据、int*复数Q_数据、int*I_数据、int*Q_数据)
{
int i=0,j=0,k=0,实和=0,imag和=0;
int*real\u part=NULL,*imag\u part=NULL,*inp\u real\u part=NULL,*inp\u imag\u part=NULL;
实部=(int*)malloc(FIR_系数*sizeof(int));
imag_part=(int*)malloc(FIR_系数*sizeof(int));
inp_real_part=(int*)malloc(FIR_系数*sizeof(int));
inp_imag_part=(int*)malloc(FIR_系数*sizeof(int));
//初始化为0。
对于(i=0;i不完整的代码,因此我无法验证,但函数shift\u array\u元素似乎超出了范围:
void shift_array_elements(int *array_data, int size)
{
int i = 0;
for(i=size;i>0;i--)
{
array_data[i] = array_data[i-1];
}
}
从i=size开始,当从fir()函数调用时,它超出了界限:
if((i < (SAMPLES-1)))
{
//printf("Reached\n");
shift_array_elements(inp_real_part,FIR_COEFFICIENT);
shift_array_elements(inp_imag_part,FIR_COEFFICIENT);
}
if((i<(示例-1)))
{
//printf(“已到达\n”);
移位数组元素(inp\u实部,FIR\u系数);
移位数组元素(inp\u imag\u部分,FIR\u系数);
}
这里的最后一个元素是inp\u实部[FIR\u系数-1]
,但是shift\u数组元素将分配给inp\u实部[FIR\u系数]
一般提示
- 使用调试器(gdb)找出程序崩溃的位置。使用
-g-O2
编译并通过gdb运行可执行文件,以从崩溃中获取堆栈跟踪
- 如果分配失败,
malloc
将返回NULL。使用malloc/free
时,请务必在继续之前检查返回值
用于(i=size;i>0;i--){array_data[i]=array_data[i-1];}
我认为您在这里写得太离谱了。为什么要使用malloc?您不能将指针设置为NULL
,然后将其释放,这没有任何意义。osiris知道NULL和then free没有任何意义,但在使用freeHow时遇到了崩溃问题。这与垃圾邮件标签有什么关系吗。
if((i < (SAMPLES-1)))
{
//printf("Reached\n");
shift_array_elements(inp_real_part,FIR_COEFFICIENT);
shift_array_elements(inp_imag_part,FIR_COEFFICIENT);
}